1 | "use strict";
|
2 | var __importDefault = (this && this.__importDefault) || function (mod) {
|
3 | return (mod && mod.__esModule) ? mod : { "default": mod };
|
4 | };
|
5 | Object.defineProperty(exports, "__esModule", { value: true });
|
6 | exports.notFound = notFound;
|
7 | exports.errorHandler = errorHandler;
|
8 | const path_1 = __importDefault(require("path"));
|
9 | const errors_1 = require("@feathersjs/errors");
|
10 | const defaults = {
|
11 | public: path_1.default.resolve(__dirname, '..', 'public'),
|
12 | logger: console
|
13 | };
|
14 | const defaultHtmlError = path_1.default.resolve(defaults.public, 'default.html');
|
15 | function notFound({ verbose = false } = {}) {
|
16 | return function (req, _res, next) {
|
17 | const url = `${req.url}`;
|
18 | const message = `Page not found${verbose ? ': ' + url : ''}`;
|
19 | next(new errors_1.NotFound(message, { url }));
|
20 | };
|
21 | }
|
22 | function errorHandler(_options = {}) {
|
23 | const options = Object.assign({}, defaults, _options);
|
24 | if (typeof options.html === 'undefined') {
|
25 | options.html = {
|
26 | 401: path_1.default.resolve(options.public, '401.html'),
|
27 | 404: path_1.default.resolve(options.public, '404.html'),
|
28 | default: defaultHtmlError
|
29 | };
|
30 | }
|
31 | if (typeof options.json === 'undefined') {
|
32 | options.json = {};
|
33 | }
|
34 | return function (error, req, res, next) {
|
35 |
|
36 | error.code = !isNaN(parseInt(error.code, 10)) ? parseInt(error.code, 10) : 500;
|
37 |
|
38 | if (options.logger && typeof options.logger.error === 'function' && !res.hook) {
|
39 | if (error.code >= 500) {
|
40 | options.logger.error(error);
|
41 | }
|
42 | else {
|
43 | options.logger.info(error);
|
44 | }
|
45 | }
|
46 | if (error.type !== 'FeathersError') {
|
47 | const oldError = error;
|
48 | error = oldError.errors
|
49 | ? new errors_1.GeneralError(oldError.message, {
|
50 | errors: oldError.errors
|
51 | })
|
52 | : new errors_1.GeneralError(oldError.message);
|
53 | if (oldError.stack) {
|
54 | error.stack = oldError.stack;
|
55 | }
|
56 | }
|
57 | const formatter = {};
|
58 |
|
59 | if (typeof options.html === 'function') {
|
60 | formatter['text/html'] = options.html;
|
61 | }
|
62 | else {
|
63 | let file = options.html[error.code];
|
64 | if (!file) {
|
65 | file = options.html.default || defaultHtmlError;
|
66 | }
|
67 |
|
68 | if (typeof file === 'function') {
|
69 | formatter['text/html'] = file;
|
70 | }
|
71 | else {
|
72 | formatter['text/html'] = function () {
|
73 | res.set('Content-Type', 'text/html');
|
74 | res.sendFile(file);
|
75 | };
|
76 | }
|
77 | }
|
78 |
|
79 | if (typeof options.json === 'function') {
|
80 | formatter['application/json'] = options.json;
|
81 | }
|
82 | else {
|
83 | const handler = options.json[error.code] || options.json.default;
|
84 |
|
85 | if (typeof handler === 'function') {
|
86 | formatter['application/json'] = handler;
|
87 | }
|
88 | else {
|
89 |
|
90 | if (error.code === 404) {
|
91 | error.stack = null;
|
92 | }
|
93 | formatter['application/json'] = function () {
|
94 | const output = Object.assign({}, error.toJSON());
|
95 | if (process.env.NODE_ENV === 'production') {
|
96 | delete output.stack;
|
97 | }
|
98 | res.set('Content-Type', 'application/json');
|
99 | res.json(output);
|
100 | };
|
101 | }
|
102 | }
|
103 | res.status(error.code);
|
104 | const contentType = req.headers['content-type'] || '';
|
105 | const accepts = req.headers.accept || '';
|
106 |
|
107 | if (contentType.indexOf('json') !== -1 || accepts.indexOf('json') !== -1) {
|
108 | formatter['application/json'](error, req, res, next);
|
109 | }
|
110 | else if (options.html && (contentType.indexOf('html') !== -1 || accepts.indexOf('html') !== -1)) {
|
111 | formatter['text/html'](error, req, res, next);
|
112 | }
|
113 | else {
|
114 |
|
115 | formatter['application/json'](error, req, res, next);
|
116 | }
|
117 | };
|
118 | }
|
119 |
|
\ | No newline at end of file |