Skip to content

Commit

Permalink
fixup! apply feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
shimks committed Aug 24, 2018
1 parent cd7d20d commit 4e180b2
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 22 deletions.
4 changes: 2 additions & 2 deletions packages/rest/src/keys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<ErrorHandlerOptions>(
'rest.sequence.actions.reject.options',
export const ERROR_WRITER_OPTIONS = BindingKey.create<ErrorHandlerOptions>(
'rest.errorWriterOptions',
);

/**
Expand Down
17 changes: 4 additions & 13 deletions packages/rest/src/providers/reject.provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Reject> {
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 {
Expand All @@ -28,12 +24,7 @@ export class RejectProvider implements Provider<Reject> {
action({request, response}: HandlerContext, error: Error) {
const err = <HttpError>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);
}
}
2 changes: 0 additions & 2 deletions packages/rest/src/rest.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import {
RejectProvider,
ParseParamsProvider,
SendProvider,
defaultErrorHandlerOptions,
} from './providers';
import {RestServer, RestServerConfig} from './rest.server';
import {DefaultSequence} from './sequence';
Expand Down Expand Up @@ -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());
}
}
Expand Down
6 changes: 1 addition & 5 deletions packages/rest/test/integration/http-handler.integration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import {
FindRouteProvider,
InvokeMethodProvider,
RejectProvider,
defaultErrorHandlerOptions,
} from '../..';
import {ControllerSpec, get} from '@loopback/openapi-v3';
import {Context} from '@loopback/context';
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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);

Expand Down

0 comments on commit 4e180b2

Please sign in to comment.