@noverachat/sdk-web 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/errors.ts ADDED
@@ -0,0 +1,59 @@
1
+ /**
2
+ * Error taxonomy mirrors noverachat-backend/src/noverachat/core/errors.py.
3
+ * Keep the list in-sync with the server.
4
+ */
5
+
6
+ export const ErrorCode = {
7
+ INTERNAL: "NC-GEN-001",
8
+ NOT_FOUND: "NC-GEN-002",
9
+ VALIDATION: "NC-GEN-003",
10
+ FORBIDDEN: "NC-GEN-004",
11
+ UNAUTHORIZED: "NC-AUTH-001",
12
+ TOKEN_EXPIRED: "NC-AUTH-002",
13
+ TOKEN_INVALID: "NC-AUTH-003",
14
+ TOKEN_REVOKED: "NC-AUTH-004",
15
+ APP_INACTIVE: "NC-APP-001",
16
+ APP_NOT_FOUND: "NC-APP-002",
17
+ USER_BANNED: "NC-USER-001",
18
+ USER_NOT_FOUND: "NC-USER-002",
19
+ ROOM_NOT_FOUND: "NC-ROOM-001",
20
+ ROOM_FROZEN: "NC-ROOM-002",
21
+ NOT_A_MEMBER: "NC-ROOM-003",
22
+ MESSAGE_TOO_LARGE: "NC-MSG-001",
23
+ MESSAGE_NOT_FOUND: "NC-MSG-002",
24
+ MESSAGE_EDIT_FORBID: "NC-MSG-003",
25
+ RATE_LIMITED: "NC-RATE-001",
26
+ } as const;
27
+
28
+ export type ErrorCodeValue = (typeof ErrorCode)[keyof typeof ErrorCode];
29
+
30
+ export class NoveraChatError extends Error {
31
+ public readonly code: string;
32
+ public readonly status: number;
33
+ public readonly details?: Record<string, unknown>;
34
+
35
+ constructor(
36
+ message: string,
37
+ code: string = ErrorCode.INTERNAL,
38
+ status: number = 0,
39
+ details?: Record<string, unknown>,
40
+ ) {
41
+ super(message);
42
+ this.name = "NoveraChatError";
43
+ this.code = code;
44
+ this.status = status;
45
+ // Under `exactOptionalPropertyTypes`, we cannot assign `undefined` to
46
+ // an optional field — leave the field unset instead.
47
+ if (details !== undefined) this.details = details;
48
+ }
49
+
50
+ static fromResponse(status: number, body: unknown): NoveraChatError {
51
+ const rec = body as { error?: { code?: string; message?: string; details?: Record<string, unknown> } };
52
+ const code = rec?.error?.code ?? ErrorCode.INTERNAL;
53
+ const msg = rec?.error?.message ?? `HTTP ${status}`;
54
+ const details = rec?.error?.details;
55
+ return details !== undefined
56
+ ? new NoveraChatError(msg, code, status, details)
57
+ : new NoveraChatError(msg, code, status);
58
+ }
59
+ }