import { Request , Response , NextFunction } from 'express' ; export class ErrorWithStatus extends Error { public statusCode : number ; public constructor ( message , statusCode ) { super ( message ) ; this . statusCode = statusCode ; } } interface ErrorWithDetails extends Error { errors : Error [ ] ; } export const errorMiddleware = ( ) => ( err , _req , res , _next ) : void => { let errors ; let responseCode = 500 ; if ( Array . isArray ( err ) ) { errors = err ; } else if ( ( err as ErrorWithDetails ) . errors ) { errors = ( err as ErrorWithDetails ) . errors ; } else { errors = [ err ] ; } for ( const error of errors ) { Object . defineProperty ( error , 'message' , { enumerable : true } ) ; } if ( err instanceof ErrorWithStatus ) { const { statusCode , ... message } = err ; errors = message ; responseCode = statusCode ; } res . status ( responseCode ) . send ( { errors } ) ; } ;