-
Notifications
You must be signed in to change notification settings - Fork 8.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Enterprise Search] Update enterpriseSearchRequestHandler to manage range of errors + add handleAPIErrors helper #77258
Merged
Merged
Changes from 3 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
e57009c
Update/refactor EnterpriseSearchRequestHandler to manage internal end…
cee-chen 0e1f2eb
Fix http interceptor bug preventing 4xx/5xx responses from being caught
cee-chen 2b949c5
Add handleAPIError helper
cee-chen ab889ee
Fix type check
cee-chen dcdd872
[Feedback] Add option to queue flash messages to handleAPIError
cee-chen 9bf8069
PR feedback: Add test case for bodies flagged as JSON which are not
cee-chen 5287c75
Merge branch 'master' into handle-api-errors
cee-chen ad4f8fd
Rename handleAPIError to flashAPIErrors
cee-chen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
50 changes: 50 additions & 0 deletions
50
...ins/enterprise_search/public/applications/shared/flash_messages/handle_api_errors.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
jest.mock('./', () => ({ | ||
FlashMessagesLogic: { actions: { setFlashMessages: jest.fn() } }, | ||
})); | ||
import { FlashMessagesLogic } from './'; | ||
|
||
import { handleAPIError } from './handle_api_errors'; | ||
|
||
describe('handleAPIError', () => { | ||
const mockHttpError = { | ||
body: { | ||
statusCode: 404, | ||
error: 'Not Found', | ||
message: 'Could not find X,Could not find Y,Something else bad happened', | ||
attributes: { | ||
errors: ['Could not find X', 'Could not find Y', 'Something else bad happened'], | ||
}, | ||
}, | ||
} as any; | ||
|
||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('converts API errors into flash messages', () => { | ||
handleAPIError(mockHttpError); | ||
|
||
expect(FlashMessagesLogic.actions.setFlashMessages).toHaveBeenCalledWith([ | ||
{ type: 'error', message: 'Could not find X' }, | ||
{ type: 'error', message: 'Could not find Y' }, | ||
{ type: 'error', message: 'Something else bad happened' }, | ||
]); | ||
}); | ||
|
||
it('displays a generic error message and re-throws non-API errors', () => { | ||
try { | ||
handleAPIError(Error('whatever') as any); | ||
} catch (e) { | ||
expect(e.message).toEqual('whatever'); | ||
expect(FlashMessagesLogic.actions.setFlashMessages).toHaveBeenCalledWith([ | ||
{ type: 'error', message: 'An unexpected error occurred' }, | ||
]); | ||
} | ||
}); | ||
}); |
46 changes: 46 additions & 0 deletions
46
.../plugins/enterprise_search/public/applications/shared/flash_messages/handle_api_errors.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import { HttpResponse } from 'src/core/public'; | ||
|
||
import { FlashMessagesLogic, IFlashMessage } from './'; | ||
|
||
/** | ||
* The API errors we are handling can come from one of two ways: | ||
* - When our http calls recieve a response containing an error code, such as a 404 or 500 | ||
* - Our own JS while handling a successful response | ||
* | ||
* In the first case, if it is a purposeful error (like a 404) we will receive an | ||
* `errors` property in the response's data, which will contain messages we can | ||
* display to the user. | ||
*/ | ||
interface IErrorResponse { | ||
statusCode: number; | ||
error: string; | ||
message: string; | ||
attributes: { | ||
errors: string[]; | ||
}; | ||
} | ||
|
||
/** | ||
* Converts API/HTTP errors into user-facing Flash Messages | ||
*/ | ||
export const handleAPIError = (error: HttpResponse<IErrorResponse>) => { | ||
const defaultErrorMessage = 'An unexpected error occurred'; | ||
|
||
const errorFlashMessages: IFlashMessage[] = Array.isArray(error?.body?.attributes?.errors) | ||
? error.body!.attributes.errors.map((message) => ({ type: 'error', message })) | ||
: [{ type: 'error', message: defaultErrorMessage }]; | ||
|
||
FlashMessagesLogic.actions.setFlashMessages(errorFlashMessages); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was actually pretty blown away that this Just Worked(™️) within both a Logic file: and a React component: Kea is amazing! 💞 |
||
|
||
// If this was a programming error or a failed request (such as a CORS) error, | ||
// we rethrow the error so it shows up in the developer console | ||
if (!error?.body?.message) { | ||
throw error; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also wanted to raise that I removed the
onErrorCallback
arg because I thought it was cleaner to have the helper be in charge of setting FlashMessages vs. passing insetFlashMessages
each time.It's possible we'll want/need to pass a custom callback in the future, but I can't think of a really useful case for a passed callback currently (esp with try/catch syntax) - can anyone else?
Another thought: since I'm making this handler specific to flash messages (I placed it in
shared/flash_messages
because I thought it made a lot of sense in there), should we consider renaming this to something likeflashAPIErrors
instead?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this approach is fine since its coupled to the messages.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rad! I went ahead renamed
handleAPIError
toflashAPIErrors
here: ad4f8fdCC @byronhulcher as a heads up!