From 4e180b286bf6a353067995795a7770522332588a Mon Sep 17 00:00:00 2001 From: shimks Date: Fri, 24 Aug 2018 12:11:54 -0400 Subject: [PATCH] fixup! apply feedback --- packages/rest/src/keys.ts | 4 ++-- packages/rest/src/providers/reject.provider.ts | 17 ++++------------- packages/rest/src/rest.component.ts | 2 -- .../integration/http-handler.integration.ts | 6 +----- 4 files changed, 7 insertions(+), 22 deletions(-) diff --git a/packages/rest/src/keys.ts b/packages/rest/src/keys.ts index 0e863de85e74..009d844232df 100644 --- a/packages/rest/src/keys.ts +++ b/packages/rest/src/keys.ts @@ -67,8 +67,8 @@ export namespace RestBindings { * Binding key for setting and injecting Reject action's error handling * options */ - export const ERROR_HANDLER_OPTIONS = BindingKey.create( - 'rest.sequence.actions.reject.options', + export const ERROR_WRITER_OPTIONS = BindingKey.create( + 'rest.errorWriterOptions', ); /** diff --git a/packages/rest/src/providers/reject.provider.ts b/packages/rest/src/providers/reject.provider.ts index ee6c1f5bc41f..f8c2e6d0e78e 100644 --- a/packages/rest/src/providers/reject.provider.ts +++ b/packages/rest/src/providers/reject.provider.ts @@ -7,18 +7,14 @@ import {LogError, Reject, HandlerContext} from '../types'; import {inject, Provider} from '@loopback/context'; import {HttpError} from 'http-errors'; import {RestBindings} from '../keys'; -import * as errorHandler from 'strong-error-handler'; - -export const defaultErrorHandlerOptions: errorHandler.ErrorHandlerOptions = { - log: false, -}; +import {writeErrorToResponse, ErrorWriterOptions} from 'strong-error-handler'; export class RejectProvider implements Provider { constructor( @inject(RestBindings.SequenceActions.LOG_ERROR) protected logError: LogError, - @inject(RestBindings.ERROR_HANDLER_OPTIONS) - protected errorHandlerOptions?: errorHandler.ErrorHandlerOptions, + @inject(RestBindings.ERROR_WRITER_OPTIONS, {optional: true}) + protected errorHandlerOptions?: ErrorWriterOptions, ) {} value(): Reject { @@ -28,12 +24,7 @@ export class RejectProvider implements Provider { action({request, response}: HandlerContext, error: Error) { const err = error; const statusCode = err.statusCode || err.status || 500; - errorHandler.writeErrorToResponse( - err, - request, - response, - this.errorHandlerOptions, // log option is intentionally not honoured here - ); + writeErrorToResponse(err, request, response, this.errorHandlerOptions); this.logError(error, statusCode, request); } } diff --git a/packages/rest/src/rest.component.ts b/packages/rest/src/rest.component.ts index d8916b0d7dbc..1204335719ca 100644 --- a/packages/rest/src/rest.component.ts +++ b/packages/rest/src/rest.component.ts @@ -21,7 +21,6 @@ import { RejectProvider, ParseParamsProvider, SendProvider, - defaultErrorHandlerOptions, } from './providers'; import {RestServer, RestServerConfig} from './rest.server'; import {DefaultSequence} from './sequence'; @@ -49,7 +48,6 @@ export class RestComponent implements Component { @inject(RestBindings.CONFIG) config?: RestComponentConfig, ) { app.bind(RestBindings.SEQUENCE).toClass(DefaultSequence); - app.bind(RestBindings.ERROR_HANDLER_OPTIONS).to(defaultErrorHandlerOptions); app.bind(RestBindings.API_SPEC).to(createEmptyApiSpec()); } } diff --git a/packages/rest/test/integration/http-handler.integration.ts b/packages/rest/test/integration/http-handler.integration.ts index 62a422d49d84..7051367c0486 100644 --- a/packages/rest/test/integration/http-handler.integration.ts +++ b/packages/rest/test/integration/http-handler.integration.ts @@ -12,7 +12,6 @@ import { FindRouteProvider, InvokeMethodProvider, RejectProvider, - defaultErrorHandlerOptions, } from '../..'; import {ControllerSpec, get} from '@loopback/openapi-v3'; import {Context} from '@loopback/context'; @@ -440,7 +439,7 @@ describe('HttpHandler', () => { }); it('respects error handler options', async () => { - rootContext.bind(RestBindings.ERROR_HANDLER_OPTIONS).to({debug: true}); + rootContext.bind(RestBindings.ERROR_WRITER_OPTIONS).to({debug: true}); const spec = anOpenApiSpec() .withOperation( @@ -482,9 +481,6 @@ describe('HttpHandler', () => { .to(createUnexpectedHttpErrorLogger()); rootContext.bind(SequenceActions.SEND).to(writeResultToResponse); rootContext.bind(SequenceActions.REJECT).toProvider(RejectProvider); - rootContext - .bind(RestBindings.ERROR_HANDLER_OPTIONS) - .to(defaultErrorHandlerOptions); rootContext.bind(RestBindings.SEQUENCE).toClass(DefaultSequence);