11"use strict" ;
2+ function __export ( m ) {
3+ for ( var p in m ) if ( ! exports . hasOwnProperty ( p ) ) exports [ p ] = m [ p ] ;
4+ }
25var __importDefault = ( this && this . __importDefault ) || function ( mod ) {
36 return ( mod && mod . __esModule ) ? mod : { "default" : mod } ;
47} ;
5- var __importStar = ( this && this . __importStar ) || function ( mod ) {
6- if ( mod && mod . __esModule ) return mod ;
7- var result = { } ;
8- if ( mod != null ) for ( var k in mod ) if ( Object . hasOwnProperty . call ( mod , k ) ) result [ k ] = mod [ k ] ;
9- result [ "default" ] = mod ;
10- return result ;
11- } ;
128Object . defineProperty ( exports , "__esModule" , { value : true } ) ;
139const ajv_1 = __importDefault ( require ( "ajv" ) ) ;
1410const fastify_plugin_1 = __importDefault ( require ( "fastify-plugin" ) ) ;
15- const http_errors_1 = __importStar ( require ( "http-errors" ) ) ;
16- const http_status_codes_1 = require ( "http-status-codes" ) ;
17- const statuses_1 = __importDefault ( require ( "statuses" ) ) ;
18- const properties_1 = require ( "./properties" ) ;
19- const utils_1 = require ( "./utils" ) ;
11+ const handlers_1 = require ( "./handlers" ) ;
2012const validation_1 = require ( "./validation" ) ;
21- var properties_2 = require ( "./properties" ) ;
22- exports . addAdditionalProperties = properties_2 . addAdditionalProperties ;
13+ __export ( require ( "./handlers" ) ) ;
14+ var properties_1 = require ( "./properties" ) ;
15+ exports . addAdditionalProperties = properties_1 . addAdditionalProperties ;
2316var validation_2 = require ( "./validation" ) ;
2417exports . convertValidationErrors = validation_2 . convertValidationErrors ;
2518exports . niceJoin = validation_2 . niceJoin ;
2619exports . validationMessagesFormatters = validation_2 . validationMessagesFormatters ;
27- function handleNotFoundError ( request , reply ) {
28- handleErrors ( new http_errors_1 . NotFound ( 'Not found.' ) , request , reply ) ;
29- }
30- exports . handleNotFoundError = handleNotFoundError ;
31- function handleValidationError ( error , request ) {
32- /*
33- As seen in
34- https://github.com/fastify/fastify/blob/master/lib/validation.js#L96
35- and
36- https://github.com/fastify/fastify/blob/master/lib/validation.js#L156,
37-
38- the error.message will always start with the relative section (params, querystring, headers, body)
39- and fastify throws on first failing section.
40- */
41- const section = error . message . match ( / ^ \w + / ) [ 0 ] ;
42- return http_errors_1 . default ( http_status_codes_1 . BAD_REQUEST , 'One or more validations failed trying to process your request.' , {
43- failedValidations : validation_1 . convertValidationErrors ( section , Reflect . get ( request , section ) , error . validation )
44- } ) ;
45- }
46- exports . handleValidationError = handleValidationError ;
47- function handleErrors ( error , request , reply ) {
48- var _a , _b ;
49- // It is a generic error, handle it
50- const code = error . code ;
51- if ( ! ( 'statusCode' in error ) ) {
52- if ( 'validation' in error && ( ( _a = request . errorProperties ) === null || _a === void 0 ? void 0 : _a . convertValidationErrors ) ) {
53- // If it is a validation error, convert errors to human friendly format
54- error = handleValidationError ( error , request ) ;
55- }
56- else if ( ( _b = request . errorProperties ) === null || _b === void 0 ? void 0 : _b . hideUnhandledErrors ) {
57- // It is requested to hide the error, just log it and then create a generic one
58- request . log . error ( { error : properties_1 . serializeError ( error ) } ) ;
59- error = new http_errors_1 . InternalServerError ( 'An error occurred trying to process your request.' ) ;
60- }
61- else {
62- // Wrap in a http-error, making the stack explicitily available
63- error = Object . assign ( new http_errors_1 . InternalServerError ( error . message ) , properties_1 . serializeError ( error ) ) ;
64- Object . defineProperty ( error , 'stack' , { enumerable : true } ) ;
65- }
66- }
67- else if ( code === 'INVALID_CONTENT_TYPE' || code === 'FST_ERR_CTP_INVALID_MEDIA_TYPE' ) {
68- error = http_errors_1 . default ( http_status_codes_1 . UNSUPPORTED_MEDIA_TYPE , utils_1 . upperFirst ( validation_1 . validationMessagesFormatters . contentType ( ) ) ) ;
69- }
70- else if ( code === 'FST_ERR_CTP_EMPTY_JSON_BODY' ) {
71- error = http_errors_1 . default ( http_status_codes_1 . BAD_REQUEST , utils_1 . upperFirst ( validation_1 . validationMessagesFormatters . jsonEmpty ( ) ) ) ;
72- }
73- else if ( code === 'MALFORMED_JSON' || error . message === 'Invalid JSON' || error . stack . includes ( 'at JSON.parse' ) ) {
74- error = http_errors_1 . default ( http_status_codes_1 . BAD_REQUEST , utils_1 . upperFirst ( validation_1 . validationMessagesFormatters . json ( ) ) ) ;
75- }
76- // Get the status code
77- let { statusCode, headers } = error ;
78- // Code outside HTTP range
79- if ( statusCode < 100 || statusCode > 599 ) {
80- statusCode = http_status_codes_1 . INTERNAL_SERVER_ERROR ;
81- }
82- // Create the body
83- const body = {
84- statusCode,
85- code : error . code ,
86- error : statuses_1 . default [ statusCode . toString ( ) ] ,
87- message : error . message
88- } ;
89- properties_1 . addAdditionalProperties ( body , error ) ;
90- // Send the error back
91- reply
92- . code ( statusCode )
93- . headers ( headers || { } )
94- . type ( 'application/json' )
95- . send ( body ) ;
96- }
97- exports . handleErrors = handleErrors ;
98- exports . default = fastify_plugin_1 . default ( function ( instance , options , done ) {
20+ exports . plugin = fastify_plugin_1 . default ( function ( instance , options , done ) {
9921 var _a , _b , _c ;
10022 const isProduction = process . env . NODE_ENV === 'production' ;
10123 const hideUnhandledErrors = ( _a = options . hideUnhandledErrors , ( _a !== null && _a !== void 0 ? _a : isProduction ) ) ;
10224 const convertValidationErrors = ( _b = options . convertValidationErrors , ( _b !== null && _b !== void 0 ? _b : true ) ) ;
10325 const convertResponsesValidationErrors = ( _c = options . convertResponsesValidationErrors , ( _c !== null && _c !== void 0 ? _c : ! isProduction ) ) ;
10426 instance . decorateRequest ( 'errorProperties' , { hideUnhandledErrors, convertValidationErrors } ) ;
105- instance . setErrorHandler ( handleErrors ) ;
106- instance . setNotFoundHandler ( handleNotFoundError ) ;
27+ instance . setErrorHandler ( handlers_1 . handleErrors ) ;
28+ instance . setNotFoundHandler ( handlers_1 . handleNotFoundError ) ;
10729 if ( convertResponsesValidationErrors ) {
10830 instance . decorate ( 'responseValidatorSchemaCompiler' , new ajv_1 . default ( {
10931 // The fastify defaults, with the exception of removeAdditional and coerceTypes, which have been reversed
@@ -117,3 +39,6 @@ exports.default = fastify_plugin_1.default(function (instance, options, done) {
11739 }
11840 done ( ) ;
11941} , { name : 'fastify-errors-properties' } ) ;
42+ // prettier-ignore
43+ module . exports = exports . plugin ;
44+ Object . assign ( module . exports , exports ) ;
0 commit comments