How would you modify fetch to handle HTTP error statuses?

WHAT IT TESTS: awareness that fetch resolves on HTTP errors and needs manual status checking. ANSWER OUTLINE: verify response.ok in then and throw if false, then catch network failures separately.
WHAT IT TESTS: whether you understand that fetch promises resolve rather than reject for HTTP error statuses such as 404 or 500, forcing the caller to inspect the response. ANSWER OUTLINE: chain a then handler that checks response.ok and throws a custom error when false, followed by a catch block that handles both network failures and thrown HTTP errors. RED FLAG: believing fetch rejects on 404, or placing all error handling only in catch without inspecting response.status.
Read the original → developer.mozilla.org
- #fetch
- #promises
- #error-handling
- #http
- #web-apis
Get five bites like this every day.
Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.