Separate HTTP, network and decoding failures
Quick answer
A robust request layer distinguishes fetch rejection, unsuccessful HTTP status, malformed body and invalid domain data. Treat these as different failure categories.
How it works
Check response.ok (status 200 through 299) before decoding. Handle 204 separately. Catch body parsing failures and validate the parsed shape. A network failure, policy block or cancellation may reject without any readable HTTP status. The examples use constructed Response objects and an explicitly injected rejecting function, not a real network.
Local examples
Run in a fresh browser document; no external network is needed. The actual ByteMind editor is unverified.
(async () => {async function decode(r){if(!r.ok)return {kind:"http",status:r.status};try{return {kind:"ok",data:await r.json()};}catch{return {kind:"decode"};}}console.log(JSON.stringify(await decode(new Response("bad",{status:503}))));})()Expected console output:
{"kind":"http","status":503}Status is classified before parsing.
Run in a fresh browser document; no external network is needed. The actual ByteMind editor is unverified.
(async () => {async function load(send){try{await send();return "ok";}catch{return "transport";}}console.log(await load(()=>Promise.reject(new TypeError("fixture"))));})()Expected console output:
transportA simulated rejection exercises the transport branch.
Common mistakes
Do not label every caught TypeError a network outage; application code can throw the same type. Preserve the operation stage.
Guided practice
Design a result type for success, HTTP error, transport error and invalid data.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Separate HTTP, network and decoding failures — Knowledge Check
Apply the concept and reason about the example output.
Which failure can occur even with HTTP status 200?
Where should an HTTP error be classified?
Why retain failure stage?
What is the expected console output of Separate HTTP, network and decoding failures example 1?
(async () => {async function decode(r){if(!r.ok)return {kind:"http",status:r.status};try{return {kind:"ok",data:await r.json()};}catch{return {kind:"decode"};}}console.log(JSON.stringify(await decode(new Response("bad",{status:503}))));})()What is the expected console output of Separate HTTP, network and decoding failures example 2?
(async () => {async function load(send){try{await send();return "ok";}catch{return "transport";}}console.log(await load(()=>Promise.reject(new TypeError("fixture"))));})()