Skip to content

Commit

Permalink
Merge pull request #4 from icapps/develop
Browse files Browse the repository at this point in the history
Merge 'develop' into 'master'
  • Loading branch information
knor-el-snor committed Mar 19, 2018
2 parents 4935a9e + 74d5f1b commit e6078a4
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 3 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "tree-house-errors",
"version": "1.0.0",
"version": "1.0.1",
"description": "NodeJS default error definitions with an error parser utility function",
"main": "build/index.js",
"types": "build/index.d.ts",
Expand Down
3 changes: 2 additions & 1 deletion src/lib/errorParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@ export function parseErrors(error: any) {

// Return object easy to use for serialisation
return {
id: parsedError.id,
status: parsedError.status,
code: parsedError.code,
title: parsedError.message,
detail: parsedError.detail || parsedError.message,
stack: error instanceof Error ? parsedError.stack : undefined, // At least add stacktrace for unknown errors
meta: error instanceof Error ? { stack: parsedError.stack } : undefined, // At least add stacktrace for unknown errors
};
}
5 changes: 4 additions & 1 deletion tests/errorParser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ describe('errorParser', () => {
it('Should return the default error when unknown object is passed', () => {
const parsedError = parseErrors({ myProp: 'iDunno' });
expect(parsedError).toMatchObject({
id: expect.any(String),
status: defaultError.status,
code: defaultError.code,
title: defaultError.message,
detail: defaultError.message,
stack: undefined,
meta: undefined,
});
});

Expand All @@ -31,6 +32,7 @@ describe('errorParser', () => {
const expressValidationError = new ValidationError([{ name: 'myField' }], options);
const parsedError = parseErrors(expressValidationError);
expect(parsedError).toMatchObject({
id: expect.any(String),
status: httpStatus.BAD_REQUEST,
code: errors.INVALID_INPUT.code,
title: errors.INVALID_INPUT.message,
Expand All @@ -45,6 +47,7 @@ describe('errorParser', () => {
} catch (err) {
const parsedError = parseErrors(err);
expect(parsedError).toMatchObject({
id: expect.any(String),
status: httpStatus.BAD_REQUEST,
code: errors.INVALID_INPUT.code,
title: errors.INVALID_INPUT.message,
Expand Down

0 comments on commit e6078a4

Please sign in to comment.