-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: [BD-26] Improve handling of API errors, refactor redux store in…
…itialization, fix multiple API calls, add new pages (#21) * [BD-26][EDUCATOR-5808] Add expired and onboarding failed pages (#37) * feat: added expired page * feat: added onboarding error page * [BD-26] Refactor API calls and redux store initialization (#38) * feat: improve handling of API errors * docs: update decision docs * tests: update tests * tests: add tests for ExamApiError component and small refactoring of tests for expired page
- Loading branch information
1 parent
5ea7bec
commit f250975
Showing
17 changed files
with
703 additions
and
74 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
3. Handling of API errors | ||
------------------------- | ||
|
||
Context | ||
------- | ||
During the development of the library we realized that sometimes the backend API could fail | ||
for some reason and we have to properly notify the user about it and show the error message, | ||
so they could contact support to get it fixed. | ||
|
||
Decision | ||
-------- | ||
The decision was made to keep a separate field in our redux store for error messages. Each time | ||
the API fails to respond with success status, the action is dispatched to populate the | ||
field with an appropriate message which in turn gets rendered in the separate component | ||
(located in src/exam/ExamAPIError.jsx). | ||
|
||
Keeping the message in redux store allows for simple usage, all you need to do | ||
is to add a catch block with error handler in it to the API call in your thunk function | ||
and it will take care of rendering the error in case there is one. | ||
|
||
The handler is located in src/data/thunks.js and looks as follows | ||
|
||
.. code-block:: javascript | ||
function handleAPIError(error, dispatch) { | ||
const { message, detail } = error; | ||
dispatch(setApiError({ errorMsg: message || detail })); | ||
} | ||
.. | ||
For example, to render an error which comes from the API directly you would do: | ||
|
||
.. code-block:: javascript | ||
try { | ||
const data = await fetchExamReviewPolicy(exam.id); | ||
} catch (error) { | ||
handleAPIError(error, dispatch); | ||
} | ||
.. | ||
if you want to render custom message: | ||
|
||
.. code-block:: javascript | ||
try { | ||
const data = await fetchExamReviewPolicy(exam.id); | ||
} catch (error) { | ||
handleAPIError({ message: 'Your custom error message' }, dispatch); | ||
} | ||
.. |
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
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.