Replies: 1 comment 1 reply
-
by calling unwrap, you are explicitly creating a promise that throws when the request fails. your middleware cannot do anything about this, it's entirely unrelated. if you don't want it to throw, don't call unwrap: triggerFetchTruckByTruckId(truckId)
.then((res) => {
if (!("data" in res)) return;
const { data } = res;
if (!data || Object.values(data).length === 0) {
serverErrorRef.current = "Truck does not exist";
} else {
//put the truck id into session storage
setSessionStorageItem("truck", JSON.stringify(Object.values(data)[0]));
history.replace(from);
}
}) |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hello
Sorry for the long question.
I summarize my "issue" first and then include some code
I have RTKQ error middle ware. it handles a custom error code (481) we get back from our apis and also any other error code like 500, etc
We were hoping that we now do not need any more catch code in our components and all error handling is taken care of in the middle ware
However when i use an unwrap/then in my component i still need a catch even if i do nothing in that catch. the reason for that is to avoid these errors in the console
Uncaught (in promise) {status: 'FETCH_ERROR', error: 'TypeError: Failed to fetch'}
i understand that when i use unwrap i get a promise and hence i seem to have to add a catch even if i do nothing in it and the middleware already took care of the error.
my question is there some way to avoid having to add that catch?
here some code. first the custom error middleware. then code that does the unwrap/then/catch where i wish i did not have to add that catch.
sample code with "useless" catch
Beta Was this translation helpful? Give feedback.
All reactions