{"record":{"id":"09d3a4a7667612c0","repo":"OpenFeign/feign","slug":"cannot-access-constructor","errorCode":null,"errorMessage":"Cannot access constructor","messagePattern":"Cannot access constructor","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"annotation-error-decoder/src/main/java/feign/error/MethodErrorHandler.java","lineNumber":56,"sourceCode":"    ExceptionGenerator constructorDefinition = getConstructorDefinition(response);\n    return createException(constructorDefinition, response);\n  }\n\n  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":38,"sourceCodeEnd":66,"githubUrl":"https://github.com/OpenFeign/feign/blob/e2a1e27560a1e68840c34f031afca88b36096e30/annotation-error-decoder/src/main/java/feign/error/MethodErrorHandler.java#L38-L66","documentation":"Thrown by MethodErrorHandler.createException (wrapped in IllegalStateException) when reflection reports IllegalAccessException while instantiating the mapped exception for an error response. The constructor exists but is not accessible from Feign's reflection code, e.g. it is not public or the class is non-public/nested.","triggerScenarios":"@FeignExceptionConstructor is on a non-public constructor; the exception class is a package-private or inner (non-static nested) class so its public constructors are still not accessible reflectively without setAccessible.","commonSituations":"Defining the custom exception as a non-static inner class of the client interface; making the constructor package-private for testing; module-system (JPMS) restrictions hiding the package.","solutions":["Make the exception class public and static (top-level or static nested) and its @FeignExceptionConstructor constructor public","If the constructor must stay non-public, ensure the class is in the same package as your decoder configuration or open the package in module-info.java","Catch IllegalStateException around the Feign call and log the cause for the underlying IllegalAccessException"],"exampleFix":"// before\nclass ApiError extends RuntimeException { // package-private\n  ApiError(int status, String body) { ... }\n}\n// after\npublic class ApiError extends RuntimeException {\n  public ApiError(int status, String body) { ... }\n}","handlingStrategy":"validation","validationCode":"int mods = ApiError.class.getModifiers();\nif (!Modifier.isPublic(mods) || Modifier.isAbstract(mods) ||\n    ApiError.class.isMemberClass() && !Modifier.isStatic(mods)) {\n  throw new IllegalStateException(\"Exception class must be public, static, non-abstract\");\n}\nif (!Modifier.isPublic(ctor.getModifiers())) throw new IllegalStateException(\"Constructor must be public\");","typeGuard":null,"tryCatchPattern":"try { api.call(); } catch (IllegalStateException e) {\n  if (\"Cannot access constructor\".equals(e.getMessage()) && e.getCause() instanceof IllegalAccessException) { ... }\n}","preventionTips":["Declare error exceptions as public top-level or public static nested classes","Keep the @FeignExceptionConstructor constructor public","Avoid package-private constructors even for testing — add a factory method instead"],"tags":["java","feign","reflection","access-modifier"],"backgroundTag":"permission-denied","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"}