11import { FastifyError , FastifyReply , FastifyRequest } from 'fastify'
22import createError , { HttpError , InternalServerError , NotFound } from 'http-errors'
3- import { BAD_REQUEST , INTERNAL_SERVER_ERROR , UNSUPPORTED_MEDIA_TYPE } from 'http-status-codes'
3+ import StatusCodes from 'http-status-codes'
44import statuses from 'statuses'
5- import { FastifyDecoratedRequest , GenericObject , NodeError , RequestSection } from './interfaces'
5+ import { GenericObject , NodeError , RequestSection } from './interfaces'
66import { addAdditionalProperties , serializeError } from './properties'
77import { upperFirst } from './utils'
88import { convertValidationErrors , validationMessagesFormatters } from './validation'
99
10- export * from './interfaces'
11- export { addAdditionalProperties } from './properties'
12- export { convertValidationErrors , niceJoin , validationMessagesFormatters } from './validation'
13-
14- export function handleNotFoundError ( request : FastifyRequest , reply : FastifyReply < unknown > ) : void {
10+ export function handleNotFoundError ( request : FastifyRequest , reply : FastifyReply ) : void {
1511 handleErrors ( new NotFound ( 'Not found.' ) , request , reply )
1612}
1713
18- export function handleValidationError ( error : FastifyError , request : FastifyRequest ) : FastifyError {
14+ export function handleValidationError ( error : FastifyError , request : FastifyRequest ) : HttpError {
1915 /*
20- As seen in
21- https://github.com/fastify/fastify/blob/master/lib/validation.js#L96
22- and
23- https://github.com/fastify/fastify/blob/master/lib/validation.js#L156,
24-
16+ As seen in https://github.com/fastify/fastify/blob/master/lib/validation.js
2517 the error.message will always start with the relative section (params, querystring, headers, body)
2618 and fastify throws on first failing section.
2719 */
2820 const section = error . message . match ( / ^ \w + / ) ! [ 0 ] as RequestSection
2921
30- return createError ( BAD_REQUEST , 'One or more validations failed trying to process your request.' , {
22+ return createError ( StatusCodes . BAD_REQUEST , 'One or more validations failed trying to process your request.' , {
3123 failedValidations : convertValidationErrors ( section , Reflect . get ( request , section ) , error . validation ! )
3224 } )
3325}
3426
35- export function handleErrors (
36- error : FastifyError ,
37- request : FastifyDecoratedRequest ,
38- reply : FastifyReply < unknown >
39- ) : void {
27+ export function handleErrors ( error : FastifyError | HttpError , request : FastifyRequest , reply : FastifyReply ) : void {
4028 // It is a generic error, handle it
4129 const code = ( error as NodeError ) . code
4230
43- if ( ! ( 'statusCode' in ( error as HttpError ) ) ) {
31+ if ( ! ( 'statusCode' in error ) ) {
4432 if ( 'validation' in error && request . errorProperties ?. convertValidationErrors ) {
4533 // If it is a validation error, convert errors to human friendly format
46- error = handleValidationError ( error , request )
34+ error = handleValidationError ( error as FastifyError , request )
4735 } else if ( request . errorProperties ?. hideUnhandledErrors ) {
4836 // It is requested to hide the error, just log it and then create a generic one
4937 request . log . error ( { error : serializeError ( error ) } )
@@ -54,32 +42,33 @@ export function handleErrors(
5442 Object . defineProperty ( error , 'stack' , { enumerable : true } )
5543 }
5644 } else if ( code === 'INVALID_CONTENT_TYPE' || code === 'FST_ERR_CTP_INVALID_MEDIA_TYPE' ) {
57- error = createError ( UNSUPPORTED_MEDIA_TYPE , upperFirst ( validationMessagesFormatters . contentType ( ) ) )
45+ error = createError ( StatusCodes . UNSUPPORTED_MEDIA_TYPE , upperFirst ( validationMessagesFormatters . contentType ( ) ) )
5846 } else if ( code === 'FST_ERR_CTP_EMPTY_JSON_BODY' ) {
59- error = createError ( BAD_REQUEST , upperFirst ( validationMessagesFormatters . jsonEmpty ( ) ) )
47+ error = createError ( StatusCodes . BAD_REQUEST , upperFirst ( validationMessagesFormatters . jsonEmpty ( ) ) )
6048 } else if ( code === 'MALFORMED_JSON' || error . message === 'Invalid JSON' || error . stack ! . includes ( 'at JSON.parse' ) ) {
61- error = createError ( BAD_REQUEST , upperFirst ( validationMessagesFormatters . json ( ) ) )
49+ error = createError ( StatusCodes . BAD_REQUEST , upperFirst ( validationMessagesFormatters . json ( ) ) )
6250 }
6351
6452 // Get the status code
6553 let { statusCode, headers } = error as HttpError
6654
6755 // Code outside HTTP range
6856 if ( statusCode < 100 || statusCode > 599 ) {
69- statusCode = INTERNAL_SERVER_ERROR
57+ statusCode = StatusCodes . INTERNAL_SERVER_ERROR
7058 }
7159
7260 // Create the body
7361 const body : GenericObject = {
7462 statusCode,
7563 code : ( error as NodeError ) . code ,
76- error : statuses [ statusCode . toString ( ) ] ,
64+ error : statuses ( statusCode . toString ( ) ) ,
7765 message : error . message
7866 }
7967
8068 addAdditionalProperties ( body , error )
8169
8270 // Send the error back
71+ // eslint-disable-next-line @typescript-eslint/no-floating-promises
8372 reply
8473 . code ( statusCode )
8574 . headers ( headers ?? { } )
0 commit comments