{"record":{"id":"e9a2200e545ad264","repo":"abi/screenshot-to-code","slug":"an-unexpected-error-occurred-exc","errorCode":null,"errorMessage":"An unexpected error occurred: {exc}","messagePattern":"An unexpected error occurred: (.+?)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"backend/image_generation/replicate.py","lineNumber":101,"sourceCode":"            response = await client.post(endpoint_url, headers=headers, json=payload)\n            response.raise_for_status()\n            response_json = response.json()\n            if not isinstance(response_json, dict):\n                raise ValueError(\"Invalid prediction creation response.\")\n\n            prediction_id = _extract_prediction_id(response_json)\n            final_response = await _poll_prediction(client, prediction_id, headers)\n            return final_response.get(\"output\")\n        except httpx.HTTPStatusError as exc:\n            raise ValueError(f\"HTTP error occurred: {exc}\") from exc\n        except httpx.RequestError as exc:\n            raise ValueError(f\"An error occurred while requesting: {exc}\") from exc\n        except asyncio.TimeoutError as exc:\n            raise TimeoutError(\"Request timed out\") from exc\n        except (TimeoutError, ValueError):\n            raise\n        except Exception as exc:\n            raise ValueError(f\"An unexpected error occurred: {exc}\") from exc\n\n\ndef _extract_output_url(result: Any, context: str) -> str:\n    if isinstance(result, str):\n        return result\n\n    if isinstance(result, dict):\n        url = cast(Any, result.get(\"url\"))\n        if isinstance(url, str) and url:\n            return url\n\n    if isinstance(result, list) and len(result) > 0:\n        first = cast(Any, result[0])\n        if isinstance(first, str) and first:\n            return first\n        if isinstance(first, Mapping):\n            url = cast(Any, first.get(\"url\"))\n            if isinstance(url, str) and url:","sourceCodeStart":83,"sourceCodeEnd":119,"githubUrl":"https://github.com/abi/screenshot-to-code/blob/d026163f586dfa8c5c10d28c36edd59a9d3b0e88/backend/image_generation/replicate.py#L83-L119","documentation":"Catch-all in _run_prediction: any exception not matching the earlier handlers (HTTPStatusError, RequestError, asyncio.TimeoutError, TimeoutError, ValueError) is re-raised as ValueError with its text, preserving the cause chain. Typical residents: JSONDecodeError when the body isn't valid JSON at all, KeyError/TypeError from unexpected payload shapes, or arbitrary bugs in the try block.","triggerScenarios":"response.json() hitting invalid JSON (HTML error pages with 200 status); a payload mutation bug inside the try block; httpx exceptions outside the two caught classes (e.g. httpx.StreamError on some setups); any programming error in the surrounding code.","commonSituations":"Proxies returning HTML with 200; partial responses cut off mid-body failing JSON parse; refactors of _run_prediction introducing a new bug that then masquerades as a generic ValueError.","solutions":["Inspect `exc.__cause__` — the original exception type tells the real story (JSONDecodeError vs TypeError vs ...).","If JSONDecodeError, log response.text to find what actually came back.","Fix the underlying bug rather than catching the generic ValueError upstream.","During debugging, temporarily re-raise the raw exception to get an unmasked traceback."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try:\n    output = await call_replicate_model(model, input, token)\nexcept ValueError as e:\n    if str(e).startswith(\"An unexpected error occurred\") and isinstance(\n        e.__cause__, json.JSONDecodeError\n    ):\n        return error(\"Replicate returned a non-JSON body — likely a proxy page\")\n    raise","preventionTips":["Always inspect __cause__ — the original exception type is the real signal","Log response.text when JSON decoding fails to identify interceptors","Fix root causes here instead of catching the generic ValueError upstream"],"tags":["replicate","error-handling","http","debugging"],"backgroundTag":null,"analyzedSha":"d026163f586dfa8c5c10d28c36edd59a9d3b0e88","analyzedAt":"2026-08-14T22:02:06.951Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}