UNPKG

1.07 kBJavaScriptView Raw
1const errors = require('@feathersjs/errors');
2
3module.exports = function errorHandler(error, reject, statusCode) {
4 if (error.ok || (statusCode && statusCode < 400)) {
5 return error;
6 }
7
8 const errMsg = error.message;
9 let feathersError = error;
10
11 if (statusCode) {
12 switch (statusCode) {
13 case 400: // Bad request
14 feathersError = new errors.BadRequest(errMsg, error);
15 break;
16 case 404: // Not found
17 feathersError = new errors.NotFound(errMsg, error);
18 break;
19 case 405: // Operation unsupported
20 feathersError = new errors.MethodNotAllowed(errMsg, error);
21 break;
22 case 409: // Unique violation
23 feathersError = new errors.Conflict(errMsg, error);
24 break;
25 case 500: // Database error
26 default:
27 feathersError = new errors.GeneralError(errMsg, error);
28 }
29 } else {
30 feathersError = new errors.GeneralError(errMsg || 'Unknown API Gateway Error', error);
31 }
32
33 if (reject)
34 return reject(feathersError);
35 else
36 return Promise.reject(feathersError);
37};