Skip to content

Commit 155b3de

Browse files
committed
refactor: improve error messages
1 parent 5ae2cc4 commit 155b3de

File tree

5 files changed

+72
-10
lines changed

5 files changed

+72
-10
lines changed

.changeset/angry-terms-tie.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@solufy/evolution-sdk": minor
3+
---
4+
5+
Improved error messages

src/api/error.ts

Lines changed: 0 additions & 8 deletions
This file was deleted.

src/api/errors.ts

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import { z } from "zod";
2+
3+
export class EvolutionApiError extends Error {
4+
constructor(message: string, cause?: unknown) {
5+
const error = getErrorMessage(cause);
6+
7+
super(message, error ? undefined : { cause });
8+
9+
this.name = EvolutionApiError.name;
10+
this.message = error ?? message;
11+
}
12+
}
13+
14+
const ErrorMessages = [
15+
ErrorMessage(
16+
z.object({
17+
message: z.array(
18+
z.object({
19+
exists: z.literal(false),
20+
jid: z.string(),
21+
number: z.string(),
22+
}),
23+
),
24+
}),
25+
"Provided number is not a valid WhatsApp number",
26+
),
27+
ErrorMessage(
28+
z.object({
29+
message: z.array(z.string().includes("Media upload failed on all hosts")),
30+
}),
31+
"Media upload failed on all hosts",
32+
),
33+
ErrorMessage(
34+
z.object({
35+
message: z.array(z.string().includes("AxiosError")),
36+
}),
37+
(response) => response.message[0],
38+
),
39+
ErrorMessage(
40+
z.object({
41+
message: z.array(z.string().includes("No session")),
42+
}),
43+
"No session found, try restarting your instance",
44+
),
45+
];
46+
47+
function getErrorMessage(response: unknown) {
48+
const error = ErrorMessages.find(
49+
(message) => message.schema.safeParse(response).success,
50+
);
51+
52+
return error
53+
? typeof error.message === "string"
54+
? error.message
55+
: // biome-ignore lint/suspicious/noExplicitAny: Generic
56+
error.message(response as any)
57+
: undefined;
58+
}
59+
60+
function ErrorMessage<T extends z.ZodType>(
61+
schema: T,
62+
message: string | ((data: z.infer<T>) => string),
63+
) {
64+
return { schema, message };
65+
}

src/api/service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { ClientOptions } from "@/schemas/client";
22
import type { APIRequestInit } from "@/types/api";
33

4-
import { EvolutionApiError } from "./error";
4+
import { EvolutionApiError } from "./errors";
55

66
export class ApiService {
77
constructor(private readonly options: ClientOptions) {}

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export class EvolutionClient {
3737
}
3838
}
3939

40-
export { EvolutionApiError } from "./api/error";
40+
export { EvolutionApiError } from "./api/errors";
4141
export { ChatId, GroupJid, Jid, MessageId } from "./types/tags";
4242
export { phoneNumberFromJid } from "./utils/phone-numer-from-jid";
4343

0 commit comments

Comments
 (0)