1
+ export class HTTPError extends Error {
2
+ constructor ( message : string , public status : number ) {
3
+ super ( `${ message } | Status: ${ status } ` ) ;
4
+ this . name = this . constructor . name ;
5
+ }
6
+ }
7
+
8
+ export class BadRequestError extends HTTPError {
9
+ constructor ( ) { super ( 'Bad Request' , 400 ) ; }
10
+ }
11
+ export class UnauthorizedError extends HTTPError {
12
+ constructor ( ) { super ( 'Unauthorized' , 401 ) ; }
13
+ }
14
+ export class PaymentRequiredError extends HTTPError {
15
+ constructor ( ) { super ( 'Payment Required' , 402 ) ; }
16
+ }
17
+ export class ForbiddenError extends HTTPError {
18
+ constructor ( ) { super ( 'Forbidden' , 403 ) ; }
19
+ }
20
+ export class NotFoundError extends HTTPError {
21
+ constructor ( ) { super ( 'Not Found' , 404 ) ; }
22
+ }
23
+ export class MethodNotAllowedError extends HTTPError {
24
+ constructor ( ) { super ( 'Method Not Allowed' , 405 ) ; }
25
+ }
26
+ export class NotAcceptableError extends HTTPError {
27
+ constructor ( ) { super ( 'Not Acceptable' , 406 ) ; }
28
+ }
29
+ export class ProxyAuthenticationRequiredError extends HTTPError {
30
+ constructor ( ) { super ( 'Proxy Authentication Required' , 407 ) ; }
31
+ }
32
+ export class RequestTimeoutError extends HTTPError {
33
+ constructor ( ) { super ( 'Request Timeout' , 408 ) ; }
34
+ }
35
+ export class ConflictError extends HTTPError {
36
+ constructor ( ) { super ( 'Conflict' , 409 ) ; }
37
+ }
38
+ export class GoneError extends HTTPError {
39
+ constructor ( ) { super ( 'Gone' , 410 ) ; }
40
+ }
41
+ export class LengthRequiredError extends HTTPError {
42
+ constructor ( ) { super ( 'Length Required' , 411 ) ; }
43
+ }
44
+ export class PreconditionFailedError extends HTTPError {
45
+ constructor ( ) { super ( 'Precondition Failed' , 412 ) ; }
46
+ }
47
+ export class PayloadTooLargeError extends HTTPError {
48
+ constructor ( ) { super ( 'Payload Too Large' , 413 ) ; }
49
+ }
50
+ export class URITooLongError extends HTTPError {
51
+ constructor ( ) { super ( 'URI Too Long' , 414 ) ; }
52
+ }
53
+ export class UnsupportedMediaTypeError extends HTTPError {
54
+ constructor ( ) { super ( 'Unsupported Media Type' , 415 ) ; }
55
+ }
56
+ export class RangeNotSatisfiableError extends HTTPError {
57
+ constructor ( ) { super ( 'Range Not Satisfiable' , 416 ) ; }
58
+ }
59
+ export class ExpectationFailedError extends HTTPError {
60
+ constructor ( ) { super ( 'Expectation Failed' , 417 ) ; }
61
+ }
62
+ export class ImATeapotError extends HTTPError {
63
+ constructor ( ) { super ( "I'm a Teapot" , 418 ) ; }
64
+ }
65
+ export class MisdirectedRequestError extends HTTPError {
66
+ constructor ( ) { super ( 'Misdirected Request' , 421 ) ; }
67
+ }
68
+ export class UnprocessableEntityError extends HTTPError {
69
+ constructor ( ) { super ( 'Unprocessable Entity' , 422 ) ; }
70
+ }
71
+ export class LockedError extends HTTPError {
72
+ constructor ( ) { super ( 'Locked' , 423 ) ; }
73
+ }
74
+ export class FailedDependencyError extends HTTPError {
75
+ constructor ( ) { super ( 'Failed Dependency' , 424 ) ; }
76
+ }
77
+ export class UpgradeRequiredError extends HTTPError {
78
+ constructor ( ) { super ( 'Upgrade Required' , 426 ) ; }
79
+ }
80
+ export class PreconditionRequiredError extends HTTPError {
81
+ constructor ( ) { super ( 'Precondition Required' , 428 ) ; }
82
+ }
83
+ export class TooManyRequestsError extends HTTPError {
84
+ constructor ( ) { super ( 'Too Many Requests' , 429 ) ; }
85
+ }
86
+ export class RequestHeaderFieldsTooLargeError extends HTTPError {
87
+ constructor ( ) { super ( 'Request Header Fields Too Large' , 431 ) ; }
88
+ }
89
+ export class UnavailableForLegalReasonsError extends HTTPError {
90
+ constructor ( ) { super ( 'Unavailable For Legal Reasons' , 451 ) ; }
91
+ }
92
+ export class InternalServerError extends HTTPError {
93
+ constructor ( ) { super ( 'Internal Server Error' , 500 ) ; }
94
+ }
95
+ export class NotImplementedError extends HTTPError {
96
+ constructor ( ) { super ( 'Not Implemented' , 501 ) ; }
97
+ }
98
+ export class BadGatewayError extends HTTPError {
99
+ constructor ( ) { super ( 'Bad Gateway' , 502 ) ; }
100
+ }
101
+ export class ServiceUnavailableError extends HTTPError {
102
+ constructor ( ) { super ( 'Service Unavailable' , 503 ) ; }
103
+ }
104
+ export class GatewayTimeoutError extends HTTPError {
105
+ constructor ( ) { super ( 'Gateway Timeout' , 504 ) ; }
106
+ }
107
+ export class HTTPVersionNotSupportedError extends HTTPError {
108
+ constructor ( ) { super ( 'HTTP Version Not Supported' , 505 ) ; }
109
+ }
110
+ export class VariantAlsoNegotiatesError extends HTTPError {
111
+ constructor ( ) { super ( 'Variant Also Negotiates' , 506 ) ; }
112
+ }
113
+ export class InsufficientStorageError extends HTTPError {
114
+ constructor ( ) { super ( 'Insufficient Storage' , 507 ) ; }
115
+ }
116
+ export class LoopDetectedError extends HTTPError {
117
+ constructor ( ) { super ( 'Loop Detected' , 508 ) ; }
118
+ }
119
+ export class NotExtendedError extends HTTPError {
120
+ constructor ( ) { super ( 'Not Extended' , 510 ) ; }
121
+ }
122
+ export class NetworkAuthenticationRequiredError extends HTTPError {
123
+ constructor ( ) { super ( 'Network Authentication Required' , 511 ) ; }
124
+ }
125
+
126
+
127
+ export function createHTTPError ( status : number ) : HTTPError {
128
+ switch ( status ) {
129
+ case 400 : return new BadRequestError ( ) ;
130
+ case 401 : return new UnauthorizedError ( ) ;
131
+ case 402 : return new PaymentRequiredError ( ) ;
132
+ case 403 : return new ForbiddenError ( ) ;
133
+ case 404 : return new NotFoundError ( ) ;
134
+ case 405 : return new MethodNotAllowedError ( ) ;
135
+ case 406 : return new NotAcceptableError ( ) ;
136
+ case 407 : return new ProxyAuthenticationRequiredError ( ) ;
137
+ case 408 : return new RequestTimeoutError ( ) ;
138
+ case 409 : return new ConflictError ( ) ;
139
+ case 410 : return new GoneError ( ) ;
140
+ case 411 : return new LengthRequiredError ( ) ;
141
+ case 412 : return new PreconditionFailedError ( ) ;
142
+ case 413 : return new PayloadTooLargeError ( ) ;
143
+ case 414 : return new URITooLongError ( ) ;
144
+ case 415 : return new UnsupportedMediaTypeError ( ) ;
145
+ case 416 : return new RangeNotSatisfiableError ( ) ;
146
+ case 417 : return new ExpectationFailedError ( ) ;
147
+ case 418 : return new ImATeapotError ( ) ;
148
+ case 421 : return new MisdirectedRequestError ( ) ;
149
+ case 422 : return new UnprocessableEntityError ( ) ;
150
+ case 423 : return new LockedError ( ) ;
151
+ case 424 : return new FailedDependencyError ( ) ;
152
+ case 426 : return new UpgradeRequiredError ( ) ;
153
+ case 428 : return new PreconditionRequiredError ( ) ;
154
+ case 429 : return new TooManyRequestsError ( ) ;
155
+ case 431 : return new RequestHeaderFieldsTooLargeError ( ) ;
156
+ case 451 : return new UnavailableForLegalReasonsError ( ) ;
157
+ case 500 : return new InternalServerError ( ) ;
158
+ case 501 : return new NotImplementedError ( ) ;
159
+ case 502 : return new BadGatewayError ( ) ;
160
+ case 503 : return new ServiceUnavailableError ( ) ;
161
+ case 504 : return new GatewayTimeoutError ( ) ;
162
+ case 505 : return new HTTPVersionNotSupportedError ( ) ;
163
+ case 506 : return new VariantAlsoNegotiatesError ( ) ;
164
+ case 507 : return new InsufficientStorageError ( ) ;
165
+ case 508 : return new LoopDetectedError ( ) ;
166
+ case 510 : return new NotExtendedError ( ) ;
167
+ case 511 : return new NetworkAuthenticationRequiredError ( ) ;
168
+ default : return new HTTPError ( 'Unknown Error' , status ) ;
169
+ }
170
+ }
171
+
0 commit comments