{"record":{"id":"40dd02aeb53f2bdc","repo":"OpenFeign/feign","slug":"cannot-invoke-constructor","errorCode":null,"errorMessage":"Cannot invoke constructor","messagePattern":"Cannot invoke constructor","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"annotation-error-decoder/src/main/java/feign/error/MethodErrorHandler.java","lineNumber":60,"sourceCode":"  private ExceptionGenerator getConstructorDefinition(Response response) {\n    if (methodLevelExceptionsByCode.containsKey(response.status())) {\n      return methodLevelExceptionsByCode.get(response.status());\n    }\n    if (classLevelExceptionsByCode.containsKey(response.status())) {\n      return classLevelExceptionsByCode.get(response.status());\n    }\n    return defaultException;\n  }\n\n  protected Exception createException(ExceptionGenerator constructorDefinition, Response response) {\n    try {\n      return constructorDefinition.createException(response);\n    } catch (IllegalAccessException e) {\n      throw new IllegalStateException(\"Cannot access constructor\", e);\n    } catch (InstantiationException e) {\n      throw new IllegalStateException(\"Cannot instantiate exception with constructor\", e);\n    } catch (InvocationTargetException e) {\n      throw new IllegalStateException(\"Cannot invoke constructor\", e);\n    } catch (NoSuchMethodException e) {\n      throw new IllegalStateException(\"Constructor does not exist\", e);\n    }\n  }\n}\n","sourceCodeStart":42,"sourceCodeEnd":66,"githubUrl":"https://github.com/OpenFeign/feign/blob/e2a1e27560a1e68840c34f031afca88b36096e30/annotation-error-decoder/src/main/java/feign/error/MethodErrorHandler.java#L42-L66","documentation":"Thrown by MethodErrorHandler.createException when InvocationTargetException is raised: the exception's constructor itself threw an exception while Feign was building the error exception for a non-2xx response. The real failure is in the constructor body, available as the cause of this IllegalStateException.","triggerScenarios":"Exception constructor that parses the response body (e.g. JSON deserialization) and throws on unexpected content; constructor preconditions (e.g. Objects.requireNonNull on body or a header) that fail for the actual error response received from the server.","commonSituations":"Server returns an HTML error page or empty body where the constructor expects JSON; a required header is absent on the error response; NPE in constructor logic for status codes not anticipated.","solutions":["Inspect the cause chain of this exception to find the constructor failure and make the constructor defensive about null/empty/malformed bodies","Guard JSON parsing in the constructor with try/catch and fall back to the raw body or status code as the message","Broaden the constructor to accept what error responses actually deliver rather than assuming a happy-path payload"],"exampleFix":"// before\npublic ApiError(String body) {\n  super(new ObjectMapper().readTree(body).get(\"message\").asText()); // throws on HTML/empty\n}\n// after\npublic ApiError(String body) {\n  super(safeMessage(body));\n}\nprivate static String safeMessage(String body) {\n  try { return new ObjectMapper().readTree(body).get(\"message\").asText(); }\n  catch (Exception e) { return body == null ? \"\" : body; }\n}","handlingStrategy":"try-catch","validationCode":"// exercise the constructor with worst-case inputs before wiring it\nArrays.asList(null, \"\", \"<html>err</html>\").forEach(b -> {\n  try { new ApiError(500, b); } catch (Exception e) { throw new AssertionError(\"ctor not defensive\", e); }\n});","typeGuard":null,"tryCatchPattern":"try { api.call(); }\ncatch (FeignException e) { /* HTTP-level */ }\ncatch (IllegalStateException e) {\n  if (\"Cannot invoke constructor\".equals(e.getMessage())) {\n    Throwable real = e.getCause().getCause(); // exception thrown by your constructor\n  }\n}","preventionTips":["Make the constructor total: never throw for null/empty/malformed bodies","Wrap body parsing in try/catch with a fallback message","Do not require headers or fields in the constructor that error responses may omit","Integration-test against MockWebServer returning HTML and empty bodies"],"tags":["java","feign","reflection","constructor-exception"],"backgroundTag":"unexpected-response-shape","analyzedSha":"e2a1e27560a1e68840c34f031afca88b36096e30","analyzedAt":"2026-09-10T12:37:37.238Z","contentChangedAt":"2026-09-10T12:37:37.238Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}