I use react
+ react-router
+ redux
. I would like to create a global thing for handling all the errors thrown in the app. Is it possible to catch also API
call errors and 401
statuses?
I thoung about something like this in Root.jsx
:
window.addEventListener("error", (e) => {});
It works for critical errors which I can log. But what if I want to serve some API
errors?
For example: I want to save some grid data, but server thrown an error that 3 of 10 given rows weren't saved (I know it's a common problem and it should be resolved using transactions, but I use Mongo
here :P
). Now I want to show a simple dialog for a user which displays error message and 3 rows that weren't saved. What if I don't have permissions and the server throws 401
status code? Probably I would like to redirect user to index or login page.
This is some hipothetical example, because I want to simplify the thing I'm currently implementing.
I'm new in React
, so I don't know the solutions for now, but if I programmed same thing in Angular
, I would use Angular's HTTP Interceptors
to solve my problem. I guess I need something similar here (or maybe it's some common solution for this?).
[EDIT]
Hmm. It seems I have found something that solves my problem: fetch-intercept
Anyway, do you know other solutions?