Skip to content

Commit

Permalink
SIAP-124: Remove metadata from errors in production
Browse files Browse the repository at this point in the history
  • Loading branch information
knor-el-snor committed Apr 24, 2018
1 parent 6250d5f commit 1ec21d9
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/lib/responder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ export const responder: { success: Function, error: Function } = {
error: (res: Response, errors: any) => {
logger.debug('Error:', errors);
const parsedError = parseErrors(errors);

if (process.env.NODE_ENV === 'production') Object.assign(parsedError, { meta: undefined }); // Do not send stacktrace in production
const serializerError = ErrorSerializer.serialize([parsedError]);

logger.error('Error response: ', serializerError);
Expand Down
24 changes: 23 additions & 1 deletion tests/lib/responder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ describe('lib/responder', () => {
responder.error(res, error);
expect(res.statusCode).toEqual(httpStatus.BAD_REQUEST);
expect(res._isJSON()).toEqual(true);
expect(JSON.parse(res._getData())).toMatchObject({
expect(JSON.parse(res._getData())).toEqual({
errors: [{
id: expect.any(String),
status: httpStatus.BAD_REQUEST,
Expand All @@ -81,5 +81,27 @@ describe('lib/responder', () => {
}],
});
});

it('Should return error without stacktrace in production', () => {
process.env.NODE_ENV = 'production';

const res = httpMocks.createResponse();
const error = new BadRequestError(errors.INVALID_INPUT, { stack: 'MYSTACK' });

responder.error(res, error);
expect(res.statusCode).toEqual(httpStatus.BAD_REQUEST);
expect(res._isJSON()).toEqual(true);
expect(JSON.parse(res._getData())).toEqual({
errors: [{
id: expect.any(String),
status: httpStatus.BAD_REQUEST,
code: errors.INVALID_INPUT.code,
title: errors.INVALID_INPUT.message,
detail: errors.INVALID_INPUT.message,
}],
});

process.env.NODE_ENV = 'test';
});
});
});

0 comments on commit 1ec21d9

Please sign in to comment.