import { inject , injectable , Next , Provider } from '@loopback/core' ; import { asMiddleware , HttpErrors , LogError , Middleware , Response , MiddlewareContext , RestBindings , RestMiddlewareGroups , } from '@loopback/rest' ; @ injectable ( asMiddleware ( { group : 'validationError' , upstreamGroups : RestMiddlewareGroups . SEND_RESPONSE , downstreamGroups : RestMiddlewareGroups . CORS , } ) ) export class ErrorHandlerMiddlewareProvider implements Provider < Middleware > { constructor ( @ inject ( RestBindings . SequenceActions . LOG_ERROR ) protected logError ) { } async value ( ) { const middleware = async ( ctx , next ) => { try { return await next ( ) ; } catch ( err ) { return this . handleError ( ctx , err ) ; } } ; return middleware ; } handleError ( context , err ) { this . logError ( err , err . statusCode , context . request ) ; throw err ; } }