Skip to content

Mock error message properly? #853

Answered by kettanaito
Geddard asked this question in Q&A
Discussion options

You must be logged in to vote

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).

fetch('https://api.com/response/500').then(res => res.json()).then(json => json.errorMessage)

The only time the .catch block gets executed with fetch 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 than window.fetch. You'd have to see the document…

Replies: 2 comments 6 replies

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
6 replies
@kettanaito
Comment options

@Geddard
Comment options

@Geddard
Comment options

@kettanaito
Comment options

@Geddard
Comment options

Answer selected by Geddard
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants