{"id":"564427a7175aa22e","repo":"square/okhttp","slug":"unexpected-code-564427","errorCode":null,"errorMessage":"Unexpected code ","messagePattern":"Unexpected code ","errorType":"exception","errorClass":"IOException","httpStatus":null,"severity":"error","filePath":"samples/guide/src/main/java/okhttp3/recipes/ParseResponseWithMoshi.java","lineNumber":36,"sourceCode":"import com.squareup.moshi.JsonAdapter;\nimport com.squareup.moshi.Moshi;\nimport java.io.IOException;\nimport java.util.Map;\nimport okhttp3.OkHttpClient;\nimport okhttp3.Request;\nimport okhttp3.Response;\n\npublic final class ParseResponseWithMoshi {\n  private final OkHttpClient client = new OkHttpClient();\n  private final Moshi moshi = new Moshi.Builder().build();\n  private final JsonAdapter<Gist> gistJsonAdapter = moshi.adapter(Gist.class);\n\n  public void run() throws Exception {\n    Request request = new Request.Builder()\n        .url(\"https://api.github.com/gists/c2a7c39532239ff261be\")\n        .build();\n    try (Response response = client.newCall(request).execute()) {\n      if (!response.isSuccessful()) throw new IOException(\"Unexpected code \" + response);\n\n      Gist gist = gistJsonAdapter.fromJson(response.body().source());\n\n      for (Map.Entry<String, GistFile> entry : gist.files.entrySet()) {\n        System.out.println(entry.getKey());\n        System.out.println(entry.getValue().content);\n      }\n    }\n  }\n\n  static class Gist {\n    Map<String, GistFile> files;\n  }\n\n  static class GistFile {\n    String content;\n  }\n","sourceCodeStart":18,"sourceCodeEnd":54,"githubUrl":"https://github.com/square/okhttp/blob/4fc083138014aba3d0078f5c26d1ce84815fa984/samples/guide/src/main/java/okhttp3/recipes/ParseResponseWithMoshi.java#L18-L54","documentation":"Recipe-level guard before parsing a GitHub gist with Moshi. After client.newCall(request).execute() for a specific gist ID, if the response is not 2xx the code throws java.io.IOException(\"Unexpected code \" + response) and never reaches gistJsonAdapter.fromJson(...). Because the next step is JSON parsing, a silent 404-with-HTML-body would otherwise produce a Moshi JsonDataException, so this guard is the recipe's way of failing fast.","triggerScenarios":"GET https://api.github.com/gists/c2a7c39532239ff261be. Fires on 404 (gist deleted or never existed), 403 (GitHub unauthenticated rate limit — 60/hr per IP), or 410. A network failure throws a different IOException before this line.","commonSituations":"The hard-coded gist ID (c2a7c39532239ff261be) being deleted by its owner; hitting the 60/hr unauthenticated limit while iterating; missing User-Agent (GitHub rejects); expecting the body to always be JSON and instead getting a 404 HTML page.","solutions":["Authenticate with a GitHub token (Authorization: Bearer <token>) to lift the rate limit.","Confirm the gist ID still exists by opening it in a browser.","Check response.code() and Content-Type before attempting JSON parsing.","Add a User-Agent header (the recipe omits one on this call)."],"exampleFix":"// before\ntry (Response response = client.newCall(request).execute()) {\n  if (!response.isSuccessful()) throw new IOException(\"Unexpected code \" + response);\n  Gist gist = gistJsonAdapter.fromJson(response.body().source());\n  ...\n}\n\n// after\ntry (Response response = client.newCall(request).execute()) {\n  if (response.code() == 404) throw new IOException(\"Gist not found: \" + request.url());\n  if (response.code() == 403) throw new IOException(\"Rate-limited by GitHub\");\n  if (!\"application/json\".equalsIgnoreCase(\n        String.valueOf(response.body().contentType()))) {\n    throw new IOException(\"Unexpected content-type: \" + response.body().contentType());\n  }\n  Gist gist = gistJsonAdapter.fromJson(response.body().source());\n  ...\n}","handlingStrategy":"validation","validationCode":"// Pre-check rate limit budget and gist existence shape\nRequest req = new Request.Builder()\n    .url(\"https://api.github.com/gists/c2a7c39532239ff261be\")\n    .header(\"Authorization\", \"Bearer \" + token)\n    .header(\"Accept\", \"application/vnd.github+json\")\n    .header(\"User-Agent\", \"MyApp/1.0\")\n    .build();\ntry (Response r = client.newCall(req).execute()) {\n  if (r.code() == 404) throw new NoSuchElementException(\"gist missing\");\n  if (r.code() == 403) throw new RateLimitedException(r.header(\"X-RateLimit-Reset\"));\n  if (!\"application/json\".equalsIgnoreCase(String.valueOf(r.body().contentType()))) {\n    throw new IllegalStateException(\"Expected JSON, got \" + r.body().contentType());\n  }\n}","typeGuard":null,"tryCatchPattern":"try (Response response = client.newCall(request).execute()) {\n  if (!response.isSuccessful()) {\n    if (response.code() == 404) throw new NoSuchElementException(\"gist not found\");\n    if (response.code() == 403) throw new RateLimitedException(response.header(\"X-RateLimit-Reset\"));\n    throw new HttpException(response.code(), response.message());\n  }\n  Gist gist = gistJsonAdapter.fromJson(response.body().source());\n}","preventionTips":["Verify Content-Type is JSON before handing the body to Moshi.","Authenticate GitHub calls and respect X-RateLimit-Reset.","Handle 404 explicitly so a deleted gist is not misreported as a parse error.","Send a User-Agent header."],"tags":["okhttp","http-status","github-api","moshi","json","rate-limit","java"],"analyzedSha":"4fc083138014aba3d0078f5c26d1ce84815fa984","analyzedAt":"2026-08-04T19:09:04.639Z","schemaVersion":2}