Mock error message properly? #853
-
Hi there, im trying to mock an error scenario with MSW, but the problem i have is that if i put for example status code 500, and then So when i do:
I only see "Request failed with status code 500". I can't find any way in the docs to override the default error message i get on the catch block. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 6 replies
-
I also tried passing the status message param, no change:
None of those two strings are anywhere to be found on the error object i get in the catch function |
Beta Was this translation helpful? Give feedback.
-
Hey, @Geddard. Note that fetch('https://api.com/response/500').then(res => res.json()).then(json => json.errorMessage) The only time the If you wish to mock a network error, use the res.networkError() utility: rest.get('/resource', (req, res, ctx) => {
return res.networkError('Custom error message')
}) Please note that whether you can access the "Custom error message" is control entirely by the request client. Solution for
|
Beta Was this translation helpful? Give feedback.
Hey, @Geddard.
Note that
window.fetch
handles any responses in the resolved promise, so the.catch
block would never be called. This includes 4xx and 5xx responses. This is done on purpose, so you could access the error response body and reference the error details (i.e. error message, or any other additional information).The only time the
.catch
block gets executed withfetch
is when a network error occurs or if the request couldn't have been otherwise made. Since you get the.catch
block called, I suspect you're using a different request client thanwindow.fetch
. You'd have to see the document…