Skip to content

Commit

Permalink
fix: parse error message from property errorMessage if not found anyw…
Browse files Browse the repository at this point in the history
…here else (#10)
  • Loading branch information
dpopp07 authored Apr 10, 2019
1 parent 0083e20 commit 4097e3f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
4 changes: 3 additions & 1 deletion lib/requestwrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ function hasStringProperty(obj: any, property: string): boolean {
/**
* Look for service error message in common places, by priority
* first look in `errors[0].message`, then in `error`, then in
* `message`
* `message`, then in `errorMessage`
* @private
* @param {Object} response - error response body received from service
* @returns {string | undefined} the error message if is was found, undefined otherwise
Expand All @@ -101,6 +101,8 @@ function parseServiceErrorMessage(response: any): string | undefined {
message = response.error;
} else if (hasStringProperty(response, 'message')) {
message = response.message;
} else if (hasStringProperty(response, 'errorMessage')) {
message = response.errorMessage;
}

return message;
Expand Down
18 changes: 13 additions & 5 deletions test/unit/requestWrapper.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ describe('formatError', () => {
],
message: 'Cant find the model.',
error: 'Model not found.',
errorMessage: 'There is just no finding this model.',
code: 404,
},
headers: {
Expand Down Expand Up @@ -48,9 +49,6 @@ describe('formatError', () => {
expect(error.name).toBe('Not Found');
expect(error.code).toBe(404);
expect(error.message).toBe('Model not found.');
expect(error.body).toBe(
'{"message":"Cant find the model.","error":"Model not found.","code":404}'
);
expect(error.headers).toEqual(basicAxiosError.response.headers);
});

Expand All @@ -61,14 +59,24 @@ describe('formatError', () => {
expect(error.name).toBe('Not Found');
expect(error.code).toBe(404);
expect(error.message).toBe('Cant find the model.');
expect(error.body).toBe('{"message":"Cant find the model.","code":404}');
expect(error.headers).toEqual(basicAxiosError.response.headers);
});

it('should get error from status text when not found', () => {
it('should get the message from errorMessage', () => {
delete basicAxiosError.response.data.message;
const error = formatError(basicAxiosError);
expect(error instanceof Error).toBe(true);
expect(error.name).toBe('Not Found');
expect(error.code).toBe(404);
expect(error.message).toBe('There is just no finding this model.');
expect(error.body).toBe('{"errorMessage":"There is just no finding this model.","code":404}');
expect(error.headers).toEqual(basicAxiosError.response.headers);
});

it('should get error from status text when not found', () => {
delete basicAxiosError.response.data.errorMessage;
const error = formatError(basicAxiosError);
expect(error instanceof Error).toBe(true);
expect(error.message).toBe('Not Found');
});

Expand Down

0 comments on commit 4097e3f

Please sign in to comment.