|
1 | | -'use strict' |
2 | | -function __export(m) { |
3 | | - for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p] |
4 | | -} |
5 | | -var __importDefault = |
6 | | - (this && this.__importDefault) || |
7 | | - function(mod) { |
8 | | - return mod && mod.__esModule ? mod : { default: mod } |
9 | | - } |
10 | | -Object.defineProperty(exports, '__esModule', { value: true }) |
11 | | -const fastify_plugin_1 = __importDefault(require('fastify-plugin')) |
12 | | -const http_errors_1 = require('http-errors') |
13 | | -const http_status_codes_1 = require('http-status-codes') |
14 | | -const statuses_1 = __importDefault(require('statuses')) |
15 | | -const properties_1 = require('./properties') |
16 | | -__export(require('./errors')) |
17 | | -var properties_2 = require('./properties') |
18 | | -exports.addAdditionalProperties = properties_2.addAdditionalProperties |
| 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 | +const fastify_plugin_1 = __importDefault(require("fastify-plugin")); |
| 7 | +const http_errors_1 = require("http-errors"); |
| 8 | +const http_status_codes_1 = require("http-status-codes"); |
| 9 | +const statuses_1 = __importDefault(require("statuses")); |
| 10 | +const properties_1 = require("./properties"); |
| 11 | +var properties_2 = require("./properties"); |
| 12 | +exports.addAdditionalProperties = properties_2.addAdditionalProperties; |
19 | 13 | function handleNotFoundError(request, reply) { |
20 | | - handleErrors(new http_errors_1.NotFound('Not found.'), request, reply) |
| 14 | + handleErrors(new http_errors_1.NotFound('Not found.'), request, reply); |
21 | 15 | } |
22 | | -exports.handleNotFoundError = handleNotFoundError |
| 16 | +exports.handleNotFoundError = handleNotFoundError; |
23 | 17 | function handleErrors(error, request, reply) { |
24 | | - var _a |
25 | | - // It is a generic error, handle it |
26 | | - if (!('statusCode' in error)) { |
27 | | - // It is requested to hide the error, just log it and then create a generic one |
28 | | - if ((_a = request.errorProperties) === null || _a === void 0 ? void 0 : _a.hideUnhandledErrors) { |
29 | | - request.log.error({ error: properties_1.serializeError(error) }) |
30 | | - error = new http_errors_1.InternalServerError('An error occurred trying to process your request.') |
31 | | - } else { |
32 | | - // Wrap in a http-error, making the stack explicitily available |
33 | | - error = Object.assign(new http_errors_1.InternalServerError(error.message), properties_1.serializeError(error)) |
34 | | - Object.defineProperty(error, 'stack', { enumerable: true }) |
| 18 | + var _a; |
| 19 | + // It is a generic error, handle it |
| 20 | + if (!('statusCode' in error)) { |
| 21 | + // It is requested to hide the error, just log it and then create a generic one |
| 22 | + if ((_a = request.errorProperties) === null || _a === void 0 ? void 0 : _a.hideUnhandledErrors) { |
| 23 | + request.log.error({ error: properties_1.serializeError(error) }); |
| 24 | + error = new http_errors_1.InternalServerError('An error occurred trying to process your request.'); |
| 25 | + } |
| 26 | + else { |
| 27 | + // Wrap in a http-error, making the stack explicitily available |
| 28 | + error = Object.assign(new http_errors_1.InternalServerError(error.message), properties_1.serializeError(error)); |
| 29 | + Object.defineProperty(error, 'stack', { enumerable: true }); |
| 30 | + } |
| 31 | + } |
| 32 | + // Get the status code |
| 33 | + let { statusCode, headers } = error; |
| 34 | + // Code outside HTTP range |
| 35 | + if (statusCode < 100 || statusCode > 599) { |
| 36 | + statusCode = http_status_codes_1.INTERNAL_SERVER_ERROR; |
35 | 37 | } |
36 | | - } |
37 | | - // Get the status code |
38 | | - let { statusCode, headers } = error |
39 | | - // Code outside HTTP range |
40 | | - if (statusCode < 100 || statusCode > 599) { |
41 | | - statusCode = http_status_codes_1.INTERNAL_SERVER_ERROR |
42 | | - } |
43 | | - // Create the body |
44 | | - const body = { |
45 | | - statusCode, |
46 | | - code: error.code, |
47 | | - error: statuses_1.default[statusCode.toString()], |
48 | | - message: error.message |
49 | | - } |
50 | | - properties_1.addAdditionalProperties(body, error) |
51 | | - // Send the error back |
52 | | - reply |
53 | | - .code(statusCode) |
54 | | - .headers(headers || {}) |
55 | | - .type('application/json') |
56 | | - .send(body) |
| 38 | + // Create the body |
| 39 | + const body = { |
| 40 | + statusCode, |
| 41 | + code: error.code, |
| 42 | + error: statuses_1.default[statusCode.toString()], |
| 43 | + message: error.message |
| 44 | + }; |
| 45 | + properties_1.addAdditionalProperties(body, error); |
| 46 | + // Send the error back |
| 47 | + reply |
| 48 | + .code(statusCode) |
| 49 | + .headers(headers || {}) |
| 50 | + .type('application/json') |
| 51 | + .send(body); |
57 | 52 | } |
58 | | -exports.handleErrors = handleErrors |
59 | | -exports.default = fastify_plugin_1.default( |
60 | | - function(instance, options, done) { |
61 | | - var _a |
62 | | - const hideUnhandledErrors = |
63 | | - ((_a = options.hideUnhandledErrors), _a !== null && _a !== void 0 ? _a : process.env.NODE_ENV === 'production') |
64 | | - instance.decorateRequest('errorProperties', { hideUnhandledErrors }) |
65 | | - instance.setErrorHandler(handleErrors) |
66 | | - instance.setNotFoundHandler(handleNotFoundError) |
67 | | - done() |
68 | | - }, |
69 | | - { name: 'fastify-errors-properties' } |
70 | | -) |
| 53 | +exports.handleErrors = handleErrors; |
| 54 | +exports.default = fastify_plugin_1.default(function (instance, options, done) { |
| 55 | + var _a; |
| 56 | + const hideUnhandledErrors = (_a = options.hideUnhandledErrors, (_a !== null && _a !== void 0 ? _a : process.env.NODE_ENV === 'production')); |
| 57 | + instance.decorateRequest('errorProperties', { hideUnhandledErrors }); |
| 58 | + instance.setErrorHandler(handleErrors); |
| 59 | + instance.setNotFoundHandler(handleNotFoundError); |
| 60 | + done(); |
| 61 | +}, { name: 'fastify-errors-properties' }); |
0 commit comments