UNPKG

6.28 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.errors = exports.Unavailable = exports.BadGateway = exports.NotImplemented = exports.GeneralError = exports.TooManyRequests = exports.Unprocessable = exports.LengthRequired = exports.Gone = exports.Conflict = exports.Timeout = exports.NotAcceptable = exports.MethodNotAllowed = exports.NotFound = exports.Forbidden = exports.PaymentError = exports.NotAuthenticated = exports.BadRequest = exports.FeathersError = void 0;
4exports.convert = convert;
5class FeathersError extends Error {
6 constructor(err, name, code, className, _data) {
7 let msg = typeof err === 'string' ? err : 'Error';
8 const properties = {
9 name,
10 code,
11 className,
12 type: 'FeathersError'
13 };
14 if (Array.isArray(_data)) {
15 properties.data = _data;
16 }
17 else if (typeof err === 'object' || _data !== undefined) {
18 const { message, errors, ...rest } = err !== null && typeof err === 'object' ? err : _data;
19 msg = message || msg;
20 properties.errors = errors;
21 properties.data = rest;
22 }
23 super(msg);
24 Object.assign(this, properties);
25 }
26 toJSON() {
27 const result = {
28 name: this.name,
29 message: this.message,
30 code: this.code,
31 className: this.className
32 };
33 if (this.data !== undefined) {
34 result.data = this.data;
35 }
36 if (this.errors !== undefined) {
37 result.errors = this.errors;
38 }
39 return result;
40 }
41}
42exports.FeathersError = FeathersError;
43class BadRequest extends FeathersError {
44 constructor(message, data) {
45 super(message, 'BadRequest', 400, 'bad-request', data);
46 }
47}
48exports.BadRequest = BadRequest;
49// 401 - Not Authenticated
50class NotAuthenticated extends FeathersError {
51 constructor(message, data) {
52 super(message, 'NotAuthenticated', 401, 'not-authenticated', data);
53 }
54}
55exports.NotAuthenticated = NotAuthenticated;
56// 402 - Payment Error
57class PaymentError extends FeathersError {
58 constructor(message, data) {
59 super(message, 'PaymentError', 402, 'payment-error', data);
60 }
61}
62exports.PaymentError = PaymentError;
63// 403 - Forbidden
64class Forbidden extends FeathersError {
65 constructor(message, data) {
66 super(message, 'Forbidden', 403, 'forbidden', data);
67 }
68}
69exports.Forbidden = Forbidden;
70// 404 - Not Found
71class NotFound extends FeathersError {
72 constructor(message, data) {
73 super(message, 'NotFound', 404, 'not-found', data);
74 }
75}
76exports.NotFound = NotFound;
77// 405 - Method Not Allowed
78class MethodNotAllowed extends FeathersError {
79 constructor(message, data) {
80 super(message, 'MethodNotAllowed', 405, 'method-not-allowed', data);
81 }
82}
83exports.MethodNotAllowed = MethodNotAllowed;
84// 406 - Not Acceptable
85class NotAcceptable extends FeathersError {
86 constructor(message, data) {
87 super(message, 'NotAcceptable', 406, 'not-acceptable', data);
88 }
89}
90exports.NotAcceptable = NotAcceptable;
91// 408 - Timeout
92class Timeout extends FeathersError {
93 constructor(message, data) {
94 super(message, 'Timeout', 408, 'timeout', data);
95 }
96}
97exports.Timeout = Timeout;
98// 409 - Conflict
99class Conflict extends FeathersError {
100 constructor(message, data) {
101 super(message, 'Conflict', 409, 'conflict', data);
102 }
103}
104exports.Conflict = Conflict;
105// 410 - Gone
106class Gone extends FeathersError {
107 constructor(message, data) {
108 super(message, 'Gone', 410, 'gone', data);
109 }
110}
111exports.Gone = Gone;
112// 411 - Length Required
113class LengthRequired extends FeathersError {
114 constructor(message, data) {
115 super(message, 'LengthRequired', 411, 'length-required', data);
116 }
117}
118exports.LengthRequired = LengthRequired;
119// 422 Unprocessable
120class Unprocessable extends FeathersError {
121 constructor(message, data) {
122 super(message, 'Unprocessable', 422, 'unprocessable', data);
123 }
124}
125exports.Unprocessable = Unprocessable;
126// 429 Too Many Requests
127class TooManyRequests extends FeathersError {
128 constructor(message, data) {
129 super(message, 'TooManyRequests', 429, 'too-many-requests', data);
130 }
131}
132exports.TooManyRequests = TooManyRequests;
133// 500 - General Error
134class GeneralError extends FeathersError {
135 constructor(message, data) {
136 super(message, 'GeneralError', 500, 'general-error', data);
137 }
138}
139exports.GeneralError = GeneralError;
140// 501 - Not Implemented
141class NotImplemented extends FeathersError {
142 constructor(message, data) {
143 super(message, 'NotImplemented', 501, 'not-implemented', data);
144 }
145}
146exports.NotImplemented = NotImplemented;
147// 502 - Bad Gateway
148class BadGateway extends FeathersError {
149 constructor(message, data) {
150 super(message, 'BadGateway', 502, 'bad-gateway', data);
151 }
152}
153exports.BadGateway = BadGateway;
154// 503 - Unavailable
155class Unavailable extends FeathersError {
156 constructor(message, data) {
157 super(message, 'Unavailable', 503, 'unavailable', data);
158 }
159}
160exports.Unavailable = Unavailable;
161exports.errors = {
162 FeathersError,
163 BadRequest,
164 NotAuthenticated,
165 PaymentError,
166 Forbidden,
167 NotFound,
168 MethodNotAllowed,
169 NotAcceptable,
170 Timeout,
171 Conflict,
172 LengthRequired,
173 Unprocessable,
174 TooManyRequests,
175 GeneralError,
176 NotImplemented,
177 BadGateway,
178 Unavailable,
179 400: BadRequest,
180 401: NotAuthenticated,
181 402: PaymentError,
182 403: Forbidden,
183 404: NotFound,
184 405: MethodNotAllowed,
185 406: NotAcceptable,
186 408: Timeout,
187 409: Conflict,
188 410: Gone,
189 411: LengthRequired,
190 422: Unprocessable,
191 429: TooManyRequests,
192 500: GeneralError,
193 501: NotImplemented,
194 502: BadGateway,
195 503: Unavailable
196};
197function convert(error) {
198 if (!error) {
199 return error;
200 }
201 const FeathersError = exports.errors[error.name];
202 const result = FeathersError
203 ? new FeathersError(error.message, error.data)
204 : new Error(error.message || error);
205 if (typeof error === 'object') {
206 Object.assign(result, error);
207 }
208 return result;
209}
210//# sourceMappingURL=index.js.map
\No newline at end of file