-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathmapper.ts
More file actions
66 lines (57 loc) · 1.65 KB
/
mapper.ts
File metadata and controls
66 lines (57 loc) · 1.65 KB
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
import { clientErrors, serverErrors, webErrors } from './index.js'
type ServerErrorCode = keyof typeof serverErrors
type ClientErrorCode = keyof typeof clientErrors
type WebErrorCode = keyof typeof webErrors
export type ErrorCode = ServerErrorCode | ClientErrorCode | WebErrorCode
export interface CatalogError {
code: string
message: string
}
const allErrors = {
...serverErrors,
...clientErrors,
...webErrors,
} as const
/**
* Maps HTTP status codes to error catalog codes
*/
export function mapHttpStatusToErrorCode(statusCode?: number): ErrorCode {
if (!statusCode || typeof statusCode !== 'number' || statusCode < 100 || statusCode > 599)
return 'UNEXPECTED_ERROR'
if (statusCode >= 200 && statusCode < 300) return 'UNEXPECTED_ERROR'
if (statusCode >= 300 && statusCode < 400) return 'UNEXPECTED_ERROR'
switch (statusCode) {
case 400:
return 'BAD_REQUEST'
case 401:
return 'UNAUTHORIZED'
case 403:
return 'FORBIDDEN'
case 404:
return 'NOT_FOUND'
case 409:
return 'CONFLICT'
case 422:
return 'INVALID_INPUT'
case 429:
return 'RATE_LIMIT_EXCEEDED'
case 500:
return 'SERVER_ERROR'
case 502:
return 'BAD_GATEWAY'
case 503:
return 'SERVICE_UNAVAILABLE'
case 504:
return 'GATEWAY_TIMEOUT'
default:
if (statusCode >= 400 && statusCode < 500) return 'BAD_REQUEST'
return 'SERVER_ERROR'
}
}
/**
* Retrieves an error from the catalog by code
*/
export function getError(code: string): CatalogError | undefined {
const error = allErrors[code as ErrorCode]
return error ? { code: error.code, message: error.message } : undefined
}