-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAppError.ts
60 lines (55 loc) · 1.24 KB
/
AppError.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
type ErrorName =
| "DB_ERROR"
| "AUTH_ERROR"
| "NOT_FOUND"
| "VALIDATION_ERROR"
| "UNKNOWN_ERROR";
export interface PlainAppError {
error: {
name: ErrorName;
message: string;
code: string;
status: number;
details: string;
hint: Record<string, any>;
};
}
export interface AppErrorOptions {
name: ErrorName;
message: string;
code?: string;
status?: number;
details?: string;
hint?: Record<string, any>;
}
// Our custom error class. Use this to pinpoint what kind of error is being returned
// from our server actions.
export class AppError extends Error {
name: ErrorName;
message: string;
code: string;
status: number;
details: string;
hint: {};
constructor(options: AppErrorOptions) {
super(options.message);
this.name = options.name;
this.message = options.message;
this.code = options.code || "UNKNOWN_CODE";
this.status = options.status || 500;
this.details = options.details || "";
this.hint = options.hint || {};
}
toPlainObject(): PlainAppError {
return {
error: {
name: this.name,
message: this.message,
code: this.code,
status: this.status,
details: this.details,
hint: this.hint,
},
};
}
}