11"use strict" ;
2- var __createBinding = ( this && this . __createBinding ) || ( Object . create ? ( function ( o , m , k , k2 ) {
3- if ( k2 === undefined ) k2 = k ;
4- Object . defineProperty ( o , k2 , { enumerable : true , get : function ( ) { return m [ k ] ; } } ) ;
5- } ) : ( function ( o , m , k , k2 ) {
6- if ( k2 === undefined ) k2 = k ;
7- o [ k2 ] = m [ k ] ;
8- } ) ) ;
9- var __setModuleDefault = ( this && this . __setModuleDefault ) || ( Object . create ? ( function ( o , v ) {
10- Object . defineProperty ( o , "default" , { enumerable : true , value : v } ) ;
11- } ) : function ( o , v ) {
12- o [ "default" ] = v ;
13- } ) ;
14- var __importStar = ( this && this . __importStar ) || function ( mod ) {
15- if ( mod && mod . __esModule ) return mod ;
16- var result = { } ;
17- if ( mod != null ) for ( var k in mod ) if ( k !== "default" && Object . prototype . hasOwnProperty . call ( mod , k ) ) __createBinding ( result , mod , k ) ;
18- __setModuleDefault ( result , mod ) ;
19- return result ;
20- } ;
21- var __importDefault = ( this && this . __importDefault ) || function ( mod ) {
22- return ( mod && mod . __esModule ) ? mod : { "default" : mod } ;
23- } ;
242Object . defineProperty ( exports , "__esModule" , { value : true } ) ;
253exports . handleErrors = exports . handleValidationError = exports . handleNotFoundError = void 0 ;
26- const http_errors_1 = __importStar ( require ( "http-errors" ) ) ;
27- const http_status_codes_1 = __importDefault ( require ( "http-status-codes" ) ) ;
28- const statuses_1 = __importDefault ( require ( "statuses" ) ) ;
29- const properties_1 = require ( "./properties" ) ;
4+ const http_errors_enhanced_1 = require ( "http-errors-enhanced" ) ;
305const utils_1 = require ( "./utils" ) ;
316const validation_1 = require ( "./validation" ) ;
327function handleNotFoundError ( request , reply ) {
33- handleErrors ( new http_errors_1 . NotFound ( 'Not found.' ) , request , reply ) ;
8+ handleErrors ( new http_errors_enhanced_1 . NotFoundError ( 'Not found.' ) , request , reply ) ;
349}
3510exports . handleNotFoundError = handleNotFoundError ;
3611function handleValidationError ( error , request ) {
@@ -40,7 +15,7 @@ function handleValidationError(error, request) {
4015 and fastify throws on first failing section.
4116 */
4217 const section = error . message . match ( / ^ \w + / ) [ 0 ] ;
43- return http_errors_1 . default ( http_status_codes_1 . default . BAD_REQUEST , 'One or more validations failed trying to process your request.' , {
18+ return new http_errors_enhanced_1 . BadRequestError ( 'One or more validations failed trying to process your request.' , {
4419 failedValidations : validation_1 . convertValidationErrors ( section , Reflect . get ( request , section ) , error . validation )
4520 } ) ;
4621}
@@ -56,38 +31,37 @@ function handleErrors(error, request, reply) {
5631 }
5732 else if ( ( _b = request . errorProperties ) === null || _b === void 0 ? void 0 : _b . hideUnhandledErrors ) {
5833 // It is requested to hide the error, just log it and then create a generic one
59- request . log . error ( { error : properties_1 . serializeError ( error ) } ) ;
60- error = new http_errors_1 . InternalServerError ( 'An error occurred trying to process your request.' ) ;
34+ request . log . error ( { error : http_errors_enhanced_1 . serializeError ( error ) } ) ;
35+ error = new http_errors_enhanced_1 . InternalServerError ( 'An error occurred trying to process your request.' ) ;
6136 }
6237 else {
63- // Wrap in a http-error , making the stack explicitily available
64- error = Object . assign ( new http_errors_1 . InternalServerError ( error . message ) , properties_1 . serializeError ( error ) ) ;
38+ // Wrap in a HttpError , making the stack explicitily available
39+ error = new http_errors_enhanced_1 . InternalServerError ( http_errors_enhanced_1 . serializeError ( error ) ) ;
6540 Object . defineProperty ( error , 'stack' , { enumerable : true } ) ;
6641 }
6742 }
6843 else if ( code === 'INVALID_CONTENT_TYPE' || code === 'FST_ERR_CTP_INVALID_MEDIA_TYPE' ) {
69- error = http_errors_1 . default ( http_status_codes_1 . default . UNSUPPORTED_MEDIA_TYPE , utils_1 . upperFirst ( validation_1 . validationMessagesFormatters . contentType ( ) ) ) ;
44+ error = new http_errors_enhanced_1 . UnsupportedMediaTypeError ( utils_1 . upperFirst ( validation_1 . validationMessagesFormatters . contentType ( ) ) ) ;
7045 }
7146 else if ( code === 'FST_ERR_CTP_EMPTY_JSON_BODY' ) {
72- error = http_errors_1 . default ( http_status_codes_1 . default . BAD_REQUEST , utils_1 . upperFirst ( validation_1 . validationMessagesFormatters . jsonEmpty ( ) ) ) ;
47+ error = new http_errors_enhanced_1 . BadRequestError ( utils_1 . upperFirst ( validation_1 . validationMessagesFormatters . jsonEmpty ( ) ) ) ;
7348 }
7449 else if ( code === 'MALFORMED_JSON' || error . message === 'Invalid JSON' || error . stack . includes ( 'at JSON.parse' ) ) {
75- error = http_errors_1 . default ( http_status_codes_1 . default . BAD_REQUEST , utils_1 . upperFirst ( validation_1 . validationMessagesFormatters . json ( ) ) ) ;
50+ error = new http_errors_enhanced_1 . BadRequestError ( utils_1 . upperFirst ( validation_1 . validationMessagesFormatters . json ( ) ) ) ;
7651 }
7752 // Get the status code
7853 let { statusCode, headers } = error ;
7954 // Code outside HTTP range
8055 if ( statusCode < 100 || statusCode > 599 ) {
81- statusCode = http_status_codes_1 . default . INTERNAL_SERVER_ERROR ;
56+ statusCode = http_errors_enhanced_1 . INTERNAL_SERVER_ERROR ;
8257 }
8358 // Create the body
8459 const body = {
8560 statusCode,
86- code : error . code ,
87- error : statuses_1 . default ( statusCode . toString ( ) ) ,
61+ error : http_errors_enhanced_1 . messagesByCodes [ statusCode ] ,
8862 message : error . message
8963 } ;
90- properties_1 . addAdditionalProperties ( body , error ) ;
64+ http_errors_enhanced_1 . addAdditionalProperties ( body , error ) ;
9165 // Send the error back
9266 // eslint-disable-next-line @typescript-eslint/no-floating-promises
9367 reply
0 commit comments