|
1 |
| -import { STATUS_CODES } from 'http' |
| 1 | +/* eslint-disable max-classes-per-file */ |
| 2 | +import { STATUS_CODES } from 'node:http' |
2 | 3 |
|
3 |
| -const statusCodes = Object.keys(STATUS_CODES).map((statusCode) => parseInt(statusCode, 10)) |
| 4 | +const statusCodes = {} |
4 | 5 |
|
5 |
| -function createErrorConstructor(errorName, statusCode) { |
6 |
| - class TaubeError extends Error { |
7 |
| - constructor(message, data) { |
8 |
| - super(message) |
9 |
| - this.name = errorName |
10 |
| - this.statusCode = statusCode |
11 |
| - this.data = data |
12 |
| - // Making it possible to detect this error as a TaubeError in other components (e.g. Server) |
13 |
| - this.isTaubeError = true |
14 |
| - } |
| 6 | +Object.keys(STATUS_CODES).forEach((code) => { |
| 7 | + const key = code |
| 8 | + const value = STATUS_CODES[key] |
| 9 | + // Map the error name without whitespace to the integer http status code |
| 10 | + statusCodes[value.replace(/\s+/g, '')] = parseInt(code, 10) |
| 11 | +}) |
| 12 | + |
| 13 | +export class TaubeError extends Error { |
| 14 | + constructor(message, data = undefined) { |
| 15 | + super(message) |
| 16 | + this.data = data |
| 17 | + // Making it possible to detect this error as a TaubeError in other components (e.g. Server) |
| 18 | + this.isTaubeError = true |
| 19 | + this.statusCode = statusCodes[this.constructor.name] |
| 20 | + this.name = this.constructor.name |
| 21 | + } |
15 | 22 |
|
16 |
| - toJSON() { |
17 |
| - return { |
18 |
| - message: this.message, |
19 |
| - name: this.name, |
20 |
| - statusCode: this.statusCode, |
21 |
| - data: this.data, |
22 |
| - } |
| 23 | + toJSON() { |
| 24 | + return { |
| 25 | + message: this.message, |
| 26 | + name: this.name, |
| 27 | + statusCode: this.statusCode, |
| 28 | + data: this.data, |
23 | 29 | }
|
24 | 30 | }
|
25 |
| - Object.defineProperty(TaubeError, 'name', { value: errorName }) |
26 |
| - return TaubeError |
27 | 31 | }
|
28 | 32 |
|
29 |
| -function populateConstructorExports(codes) { |
30 |
| - const generatedErrors = {} |
31 |
| - codes.filter((code) => code >= 400 && code <= 511) |
32 |
| - .forEach((statusCode) => { |
33 |
| - // remove space between string to make class name properly |
34 |
| - // eg., Bad Request->BadRequest |
35 |
| - const errorName = STATUS_CODES[statusCode].replace(/\s+/g, '') |
36 |
| - const error = createErrorConstructor(errorName, statusCode) |
37 |
| - generatedErrors[errorName] = error |
38 |
| - generatedErrors[statusCode] = error |
39 |
| - }) |
40 |
| - return generatedErrors |
| 33 | +const errorsStrings = { |
| 34 | + 'BadRequest': class extends TaubeError {}, |
| 35 | + 'Continue': class extends TaubeError {}, |
| 36 | + 'SwitchingProtocols': class extends TaubeError {}, |
| 37 | + 'Processing': class extends TaubeError {}, |
| 38 | + 'EarlyHints': class extends TaubeError {}, |
| 39 | + 'OK': class extends TaubeError {}, |
| 40 | + 'Created': class extends TaubeError {}, |
| 41 | + 'Accepted': class extends TaubeError {}, |
| 42 | + 'Non-AuthoritativeInformation': class extends TaubeError {}, |
| 43 | + 'NoContent': class extends TaubeError {}, |
| 44 | + 'ResetContent': class extends TaubeError {}, |
| 45 | + 'PartialContent': class extends TaubeError {}, |
| 46 | + 'Multi-Status': class extends TaubeError {}, |
| 47 | + 'AlreadyReported': class extends TaubeError {}, |
| 48 | + 'IMUsed': class extends TaubeError {}, |
| 49 | + 'MultipleChoices': class extends TaubeError {}, |
| 50 | + 'MovedPermanently': class extends TaubeError {}, |
| 51 | + 'Found': class extends TaubeError {}, |
| 52 | + 'SeeOther': class extends TaubeError {}, |
| 53 | + 'NotModified': class extends TaubeError {}, |
| 54 | + 'UseProxy': class extends TaubeError {}, |
| 55 | + 'TemporaryRedirect': class extends TaubeError {}, |
| 56 | + 'PermanentRedirect': class extends TaubeError {}, |
| 57 | + 'Unauthorized': class extends TaubeError {}, |
| 58 | + 'PaymentRequired': class extends TaubeError {}, |
| 59 | + 'Forbidden': class extends TaubeError {}, |
| 60 | + 'NotFound': class extends TaubeError {}, |
| 61 | + 'MethodNotAllowed': class extends TaubeError {}, |
| 62 | + 'NotAcceptable': class extends TaubeError {}, |
| 63 | + 'ProxyAuthenticationRequired': class extends TaubeError {}, |
| 64 | + 'RequestTimeout': class extends TaubeError {}, |
| 65 | + 'Conflict': class extends TaubeError {}, |
| 66 | + 'Gone': class extends TaubeError {}, |
| 67 | + 'LengthRequired': class extends TaubeError {}, |
| 68 | + 'PreconditionFailed': class extends TaubeError {}, |
| 69 | + 'PayloadTooLarge': class extends TaubeError {}, |
| 70 | + 'URITooLong': class extends TaubeError {}, |
| 71 | + 'UnsupportedMediaType': class extends TaubeError {}, |
| 72 | + 'RangeNotSatisfiable': class extends TaubeError {}, |
| 73 | + 'ExpectationFailed': class extends TaubeError {}, |
| 74 | + "I'maTeapot": class extends TaubeError {}, |
| 75 | + 'MisdirectedRequest': class extends TaubeError {}, |
| 76 | + 'UnprocessableEntity': class extends TaubeError {}, |
| 77 | + 'Locked': class extends TaubeError {}, |
| 78 | + 'FailedDependency': class extends TaubeError {}, |
| 79 | + 'TooEarly': class extends TaubeError {}, |
| 80 | + 'UpgradeRequired': class extends TaubeError {}, |
| 81 | + 'PreconditionRequired': class extends TaubeError {}, |
| 82 | + 'TooManyRequests': class extends TaubeError {}, |
| 83 | + 'RequestHeaderFieldsTooLarge': class extends TaubeError {}, |
| 84 | + 'UnavailableForLegalReasons': class extends TaubeError {}, |
| 85 | + 'InternalServerError': class extends TaubeError {}, |
| 86 | + 'NotImplemented': class extends TaubeError {}, |
| 87 | + 'BadGateway': class extends TaubeError {}, |
| 88 | + 'ServiceUnavailable': class extends TaubeError {}, |
| 89 | + 'GatewayTimeout': class extends TaubeError {}, |
| 90 | + 'HTTPVersionNotSupported': class extends TaubeError {}, |
| 91 | + 'VariantAlsoNegotiates': class extends TaubeError {}, |
| 92 | + 'InsufficientStorage': class extends TaubeError {}, |
| 93 | + 'LoopDetected': class extends TaubeError {}, |
| 94 | + 'BandwidthLimitExceeded': class extends TaubeError {}, |
| 95 | + 'NotExtended': class extends TaubeError {}, |
| 96 | + 'NetworkAuthenticationRequired': class extends TaubeError {}, |
| 97 | +} |
| 98 | +const errorsByStatusCodes = { |
| 99 | + '400': errorsStrings.BadRequest, |
| 100 | + '401': errorsStrings.Unauthorized, |
| 101 | + '402': errorsStrings.PaymentRequired, |
| 102 | + '403': errorsStrings.Forbidden, |
| 103 | + '404': errorsStrings.NotFound, |
| 104 | + '405': errorsStrings.MethodNotAllowed, |
| 105 | + '406': errorsStrings.NotAcceptable, |
| 106 | + '407': errorsStrings.ProxyAuthenticationRequired, |
| 107 | + '408': errorsStrings.RequestTimeout, |
| 108 | + '409': errorsStrings.Conflict, |
| 109 | + '410': errorsStrings.Gone, |
| 110 | + '411': errorsStrings.LengthRequired, |
| 111 | + '412': errorsStrings.PreconditionFailed, |
| 112 | + '413': errorsStrings.PayloadTooLarge, |
| 113 | + '414': errorsStrings.URITooLong, |
| 114 | + '415': errorsStrings.UnsupportedMediaType, |
| 115 | + '416': errorsStrings.RangeNotSatisfiable, |
| 116 | + '417': errorsStrings.ExpectationFailed, |
| 117 | + '421': errorsStrings.MisdirectedRequest, |
| 118 | + '422': errorsStrings.UnprocessableEntity, |
| 119 | + '423': errorsStrings.Locked, |
| 120 | + '424': errorsStrings.FailedDependency, |
| 121 | + '425': errorsStrings.TooEarly, |
| 122 | + '426': errorsStrings.UpgradeRequired, |
| 123 | + '428': errorsStrings.PreconditionRequired, |
| 124 | + '429': errorsStrings.TooManyRequests, |
| 125 | + '431': errorsStrings.RequestHeaderFieldsTooLarge, |
| 126 | + '451': errorsStrings.UnavailableForLegalReasons, |
| 127 | + '500': errorsStrings.InternalServerError, |
| 128 | + '501': errorsStrings.NotImplemented, |
| 129 | + '502': errorsStrings.BadGateway, |
| 130 | + '503': errorsStrings.ServiceUnavailable, |
| 131 | + '504': errorsStrings.GatewayTimeout, |
| 132 | + '505': errorsStrings.HTTPVersionNotSupported, |
| 133 | + '506': errorsStrings.VariantAlsoNegotiates, |
| 134 | + '507': errorsStrings.InsufficientStorage, |
| 135 | + '508': errorsStrings.LoopDetected, |
| 136 | + '509': errorsStrings.BandwidthLimitExceeded, |
| 137 | + '510': errorsStrings.NotExtended, |
| 138 | + '511': errorsStrings.NetworkAuthenticationRequired, |
41 | 139 | }
|
42 | 140 |
|
43 |
| -const errors = populateConstructorExports(statusCodes) |
44 |
| - |
| 141 | +const errors = { |
| 142 | + ...errorsStrings, |
| 143 | + ...errorsByStatusCodes, |
| 144 | +} |
45 | 145 | /**
|
46 | 146 | * Converts known errors to a taube error. Returns original error for unknown errors
|
47 | 147 | *
|
|
0 commit comments