{"record":{"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/91a8b34c6f44bd28c421364f8edadc9f324dddd9/samples/guide/src/main/java/okhttp3/recipes/ParseResponseWithMoshi.java#L18-L54","documentation":"Thrown in the Moshi-parsing recipe: `if (!response.isSuccessful()) throw new IOException(\"Unexpected code \" + response)`. The call hits a specific GitHub Gist by ID. If the gist was deleted, never existed, or the unauthenticated rate limit is exceeded, GitHub returns non-2xx and this throws before Moshi ever sees the body.","triggerScenarios":"GET https://api.github.com/gists/c2a7c39532239ff261be returns 404 (gist deleted/never existed), 403 (rate limit), or 410. The hardcoded gist ID was a real sample at authoring time but may no longer exist.","commonSituations":"The sample gist ID was deleted by its owner; rate-limited unauthenticated GitHub access; copy-pasting without swapping in your own gist ID; GitHub API deprecation returning a different shape.","solutions":["Verify the gist ID still exists (open it in a browser or curl -I the URL).","Swap in a gist ID you control for a stable demo.","Add a GitHub token to avoid 403 rate limiting.","Branch on response.code(); only call gistJsonAdapter.fromJson on success."],"exampleFix":"// before\nif (!response.isSuccessful()) throw new IOException(\"Unexpected code \" + response);\nGist gist = gistJsonAdapter.fromJson(response.body().source());\n\n// after\nif (!response.isSuccessful()) {\n  throw new IOException(\"Cannot fetch gist: HTTP \" + response.code());\n}\nGist gist = gistJsonAdapter.fromJson(response.body().source());","handlingStrategy":"validation","validationCode":"// Validate status before Moshi parses; only parse on success.\ntry (Response response = client.newCall(request).execute()) {\n  if (response.code() == 404) { /* gist gone */ return; }\n  if (!response.isSuccessful()) { throw new IOException(\"gist HTTP \" + response.code()); }\n  Gist gist = gistJsonAdapter.fromJson(response.body().source());\n}","typeGuard":"static boolean parseable(Response r) { return r.isSuccessful() && r.body() != null; }","tryCatchPattern":"try {\n  // call + parse\n} catch (IOException e) {\n  // status failure ('Unexpected code') or transport\n} catch (JsonDataException e) {\n  // Moshi could not map the body to Gist (different concern; parse, not status)\n}","preventionTips":["Use your own gist ID for a stable sample.","Only invoke the JsonAdapter on successful responses.","Add a GitHub token to avoid 403 rate limiting.","Check response.code() before parsing."],"tags":["okhttp","http-status","github-api","moshi","java"],"backgroundTag":null,"analyzedSha":"91a8b34c6f44bd28c421364f8edadc9f324dddd9","analyzedAt":"2026-08-10T18:39:54.316Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}