Charlie PHP Expert - Volume 11B | 1 CHARLIE PHP EXPERT Volume 11B - Regression Guard Canonical Browser Fetch / HTTP / JSON Failure Handling Purpose: Prevent regression after Volume 11A. Charlie has repeatedly demonstrated the correct model when directly tested, then reverted to an older incorrect pattern during broader Volume 11 questions. This supplement makes the corrected pattern explicit and dominant. Critical override: For browser-side JavaScript questions involving fetch(), HTTP status handling, or JSON parsing, this volume's operation-based failure model overrides any older pattern that classifies failures by TypeError, error name, error message, PHP syntax, or a single generic catch block. 1. Non-Negotiable Three-Stage Model Stage 1 - Transport/network: execute await fetch() in its own try/catch. If it throws before a Response exists, classify the outcome as network/transport. Stage 2 - HTTP status: once a Response exists, inspect response.ok or response.status. HTTP 4xx/5xx is an HTTP outcome, not a network failure. Stage 3 - JSON parsing: only after a successful HTTP response, execute await response.json() in a separate try/catch. If parsing throws, classify the outcome as a JSON/body contract failure. 2. Forbidden Regression Patterns Do not use error instanceof TypeError to identify invalid JSON. Do not use error.name, error.message, or environment-specific codes to decide which of the three boundaries failed when the boundary can be identified by which operation failed. Do not use PHP operators or syntax in browser JavaScript. Do not place fetch, HTTP classification, and JSON parsing into one undifferentiated catch path. 3. Why Operation Boundaries Are Better Than Error Labels The code already knows which operation is running. If fetch() throws inside the network try/catch, the failure happened before a Response existed. If response.ok is false, a Response exists and the failure is HTTP-level. If response.json() throws in the parse try/catch, the body failed the JSON contract. This is stronger evidence than guessing from an exception class or message. 4. Canonical Reference Implementation async function requestJson(url, options = {}) { let response; // 1) NETWORK / TRANSPORT try { response = await fetch(url, options); } catch (error) { return { ok: false, kind: "network", message: "No HTTP response was obtained.", cause: error }; } // 2) HTTP STATUS if (!response.ok) { let body = null; Charlie PHP Expert - Volume 11B | 2 try { body = await response.text(); } catch (_) { // Body reading is secondary; HTTP status is already known. } return { ok: false, kind: "http", status: response.status, statusText: response.statusText, body }; } // 3) JSON PARSING try { const data = await response.json(); return { ok: true, kind: "success", status: response.status, data }; } catch (error) { return { ok: false, kind: "json", status: response.status, message: "Response body was not valid JSON.", cause: error }; } } 5. Required Reasoning Language When explaining the code, Charlie should say: a network failure is known because fetch() failed before a Response existed; an HTTP failure is known because a Response exists and its status is not successful; a JSON failure is known because parsing the received body failed. Charlie should not claim that fetch() rejects merely because the server returned HTTP 500. 6. Valid JSON Is Not Necessarily an Object Valid JSON may represent an object, array, string, number, boolean, or null. Therefore, typeof data !== 'object' is not a malformed-JSON test. Malformed JSON is detected when the parse operation fails. 7. PHP Server Trust Boundary Browser validation is not authoritative. PHP must independently validate required fields, types, lengths, ranges, formats, business rules, authentication, authorization, tenant scope, duplicate protection where applicable, and data-integrity constraints. Client-side checks improve user experience; server-side checks enforce trust. 8. API Contract Debugging If JavaScript expects {success: true, data: ...} but PHP returns {status: 'ok', result: ...}, treat that as a contract mismatch. Inspect the actual response, identify the intended contract, update one side or both to a single documented schema, and add tests so the mismatch does not recur. 9. Self-Check Before Answering Before returning browser JavaScript, Charlie must internally verify: (1) Is the code valid JavaScript? (2) Did I accidentally use PHP syntax? (3) Is fetch() isolated from JSON parsing? (4) Is HTTP status handled after a Response exists? (5) Did I avoid TypeError/name/message classification? (6) Did I preserve server-side validation requirements? Charlie PHP Expert - Volume 11B | 3 10. Regression Rule If a broader question mixes PHP and JavaScript, Charlie must still preserve the corrected browser-side failure model. Volume 11B is a regression guard: the older incorrect pattern must not reappear simply because more topics are included in the same question. Charlie PHP Expert - Volume 11B | 4 11. Certification Retest Prompt: A PHP endpoint is supposed to return JSON. In the browser, fetch() sometimes fails before any Response exists, sometimes receives HTTP 500, and sometimes receives HTTP 200 with malformed JSON. Write valid browser JavaScript using async/await that keeps all three outcomes separate. Then explain what PHP must still validate on the server and how you would debug a contract mismatch where JavaScript expects {success: true, data: ...} but PHP returns {status: 'ok', result: ...}. Area Pass condition Network fetch() has its own try/catch; failure means no Response was obtained. HTTP response.ok/status is checked after a Response exists. JSON response.json() has its own separate try/catch. No regression No TypeError/name/message classification and no typeof-object JSON test. Language Valid browser JavaScript only; no PHP syntax leakage. Server trust PHP validates independently and enforces authorization/business rules. Contract debug Identifies schema mismatch and proposes one documented/tested contract. Certification rule: Volume 11B passes only if the corrected pattern survives inside a broader Volume 11 question. A focused answer that passes 11A but regresses when PHP, validation, or contract debugging is added does not pass regression.