-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherror.ts
125 lines (112 loc) Β· 3.13 KB
/
error.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import { isServer } from '@tanstack/react-query'
export enum ERROR_TYPE {
NOT_IMPLIMENT = 'NOT_IMPLIMENT',
BAD_REQUEST_ERROR = 'BAD_REQUEST_ERROR',
INTERNAL_SERVER_ERROR = 'INTERNAL_SERVER_ERROR',
UNAUTHORIZED = 'UNAUTHORIZED',
}
export const ERROR_MESSAGE: { [key in ERROR_TYPE]: string } = {
NOT_IMPLIMENT: 'μ μμ€μ΄μμ.',
BAD_REQUEST_ERROR: 'μλΉμ€μ λ¬Έμ κ° μκ²Όμ΄μ.',
INTERNAL_SERVER_ERROR: 'μλΉμ€μ λ¬Έμ κ° μκ²Όμ΄μ.',
UNAUTHORIZED: 'κΆνμ΄ μμ΄μ.',
}
export const getErrorMessage = <T extends string>(key: T) =>
Object.hasOwn(ERROR_MESSAGE, key)
? ERROR_MESSAGE[key as keyof typeof ERROR_MESSAGE]
: 'λ¬Έμ κ° μκ²Όμ΄μ.'
/**
* @description λ¨μμν€ λ² μ΄μ€ μλ¬ ν΄λμ€
*/
export class NamuiError extends Error {
public static readonly message: string
constructor(
public readonly message: string,
protected readonly options: ErrorOptions,
) {
super(message, options)
}
}
export class NotImplimentError extends NamuiError {
public name = ERROR_TYPE.NOT_IMPLIMENT
constructor(
public readonly message: string = 'μΏ κ΅¬νμ€!',
protected readonly options: ErrorOptions = {
cause: ERROR_TYPE.NOT_IMPLIMENT,
},
) {
super(message, options)
}
}
export class BadRequestError extends NamuiError {
public name = ERROR_TYPE.BAD_REQUEST_ERROR
constructor(
public readonly message: string = 'μλΉμ€κ° μν νμ§ μμ΅λλ€.',
protected readonly options: ErrorOptions = {
cause: ERROR_TYPE.BAD_REQUEST_ERROR,
},
) {
super(message, options)
}
}
export class UnauthorizedError extends NamuiError {
public name = ERROR_TYPE.UNAUTHORIZED
constructor(
public readonly message: string = 'κΆνμ΄ μμ΄μ.',
protected readonly options: ErrorOptions = {
cause: ERROR_TYPE.UNAUTHORIZED,
},
) {
super(message, options)
}
}
export class InternalServerError extends NamuiError {
public name = ERROR_TYPE.INTERNAL_SERVER_ERROR
constructor(
public readonly message: string = 'μλΉμ€μ λ¬Έμ κ° λ°μνμ΄μ.',
protected readonly options: ErrorOptions = {
cause: ERROR_TYPE.INTERNAL_SERVER_ERROR,
},
) {
super(message, options)
}
}
/**
*
* @param obj λ¨μμν€ μλ¬ μΈμ€ν΄μ€ νμΈ νλΌλ―Έν°
* @returns {boolean} λ¨μμν€ μλ¬ μΈμ€ν΄μ€λ©΄ true
*/
export const isNamuiError = (obj: unknown): obj is NamuiError => {
return obj instanceof NamuiError
}
export const raiseNamuiErrorFromStatus = (
status: number = 500,
isReturned: boolean = false,
) => {
const parsedError = (error: NamuiError) => {
if (isReturned) return error
if (isServer) {
try {
throw error
} catch (err) {
console.log(error.stack)
return 1
}
}
throw error
}
switch (true) {
case !status:
parsedError(new InternalServerError())
break
case status === 401:
parsedError(new UnauthorizedError())
break
case status && status > 401 && status < 500:
parsedError(new BadRequestError())
break
default:
parsedError(new InternalServerError())
break
}
}