Skip to content

Commit

Permalink
Add fallback for http errors without an errors array
Browse files Browse the repository at this point in the history
- all http error messages should still be expected to contain a message key, so should be a reliable fallback
  • Loading branch information
cee-chen committed Sep 16, 2020
1 parent 268cb6d commit 2128958
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,20 @@ describe('flashAPIErrors', () => {
]);
});

it('falls back to the basic message for http responses without an errors array', () => {
flashAPIErrors({
body: {
statusCode: 404,
error: 'Not Found',
message: 'Not Found',
},
} as any);

expect(FlashMessagesLogic.actions.setFlashMessages).toHaveBeenCalledWith([
{ type: 'error', message: 'Not Found' },
]);
});

it('displays a generic error message and re-throws non-API errors', () => {
try {
flashAPIErrors(Error('whatever') as any);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export const flashAPIErrors = (

const errorFlashMessages: IFlashMessage[] = Array.isArray(error?.body?.attributes?.errors)
? error.body!.attributes.errors.map((message) => ({ type: 'error', message }))
: [{ type: 'error', message: defaultErrorMessage }];
: [{ type: 'error', message: error?.body?.message || defaultErrorMessage }];

if (isQueued) {
FlashMessagesLogic.actions.setQueuedMessages(errorFlashMessages);
Expand Down

0 comments on commit 2128958

Please sign in to comment.