-
Notifications
You must be signed in to change notification settings - Fork 731
Expand file tree
/
Copy pathhttp.ts
More file actions
106 lines (86 loc) · 2.19 KB
/
Copy pathhttp.ts
File metadata and controls
106 lines (86 loc) · 2.19 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
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
/**
* Base class for HTTP errors with structured JSON responses.
* Subclasses must define a `code` and `status`.
*/
export abstract class HttpError extends Error {
abstract readonly code: string
abstract readonly status: number
constructor(message: string) {
super(message)
this.name = this.constructor.name
Object.setPrototypeOf(this, new.target.prototype)
}
toJSON() {
return {
error: {
code: this.code,
message: this.message,
},
}
}
}
export class BadRequestError extends HttpError {
readonly code = 'BAD_REQUEST'
readonly status = 400
constructor(message = 'Bad request') {
super(message)
}
}
export class UnauthorizedError extends HttpError {
readonly code = 'UNAUTHORIZED'
readonly status = 401
constructor(message = 'Unauthorized') {
super(message)
}
}
export class ForbiddenError extends HttpError {
readonly code = 'FORBIDDEN'
readonly status = 403
constructor(message = 'Forbidden') {
super(message)
}
}
export class InsufficientScopeError extends HttpError {
readonly code = 'INSUFFICIENT_SCOPE'
readonly status = 403
constructor(message = 'Insufficient scope for this operation') {
super(message)
}
}
export class NotFoundError extends HttpError {
readonly code = 'NOT_FOUND'
readonly status = 404
constructor(message = 'Not found') {
super(message)
}
}
export class ConflictError extends HttpError {
readonly code = 'CONFLICT'
readonly status = 409
readonly context?: Record<string, unknown>
constructor(message = 'Conflict', context?: Record<string, unknown>) {
super(message)
this.context = context
}
}
export class NotImplementedError extends HttpError {
readonly code = 'NOT_IMPLEMENTED'
readonly status = 501
constructor(message = 'Not implemented') {
super(message)
}
}
export class RateLimitError extends HttpError {
readonly code = 'RATE_LIMITED'
readonly status = 429
constructor(message = 'Too many requests, please try again later') {
super(message)
}
}
export class InternalError extends HttpError {
readonly code = 'INTERNAL_ERROR'
readonly status = 500
constructor(message = 'Internal server error') {
super(message)
}
}