import HttpStatus from '@xpring-eng/http-status' ; import { Request , Response , NextFunction , RequestHandler } from 'express' ; import metrics from '../services/metrics' ; import { PayIDError , handleHttpError , ParseErrorType , LookupErrorType , LookupError , } from '../utils/errors' ; param err param _req param res param next returns export default function errorHandler ( err , _req , res , next ) { if ( res . headersSent ) { return next ( err ) ; } let status = HttpStatus . InternalServerError ; if ( err instanceof PayIDError ) { status = err . httpStatusCode ; if ( err . kind === ParseErrorType . InvalidMediaType ) { metrics . recordPayIdLookupBadAcceptHeader ( ) ; } if ( err . kind === LookupErrorType . MissingPayId && err instanceof LookupError && err . headers ) { err . headers . forEach ( ( acceptType ) => metrics . recordPayIdLookupResult ( false , acceptType . paymentNetwork , acceptType . environment ) ) ; } } return handleHttpError ( status , err . message , res , err ) ; } param handler returns export function wrapAsync ( handler ) { return async ( req , res , next ) : Promise < void > => Promise . resolve ( handler ( req , res , next ) ) . catch ( next ) ; }