OpenAPITools / OpenAPITools/openapi-generator

[REQ] [Java] [Native] Allow exception customization without templating

Open
#8,759 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Enhancement: Feature
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Is your feature request related to a problem? Please describe.

When an exception is thrown from the xxxApi classes, the response body is stored in the exception but is not part of the message. In many cases, the body is important and contains the reason of the error.
The current implementation requires us to wrap every API call with try-catch that thrown a wrapping exception with the body in the message.

Describe the solution you'd like

Create an ExceptionHandler interface to allow user to replace the exceptions thrown and add a default ExceptionHandlerImpl that keeps the current behavior. Something like:

public interface ExceptionHandler {

  ApiException createUnsuccessfulResponseException(String msg, HttpResponse<InputStream> response);

  ApiException createException(Exception e);

  ApiException createInvalidRequestException(int statusCode, String msg);
}

The user will be able to replace the exception handler implementation by setting the exception handler on the ApiClient. Similar to how it works with the request and response interceptors.

Describe alternatives you've considered

  1. A simpler alternative which is also fine by me is to simple concatenate the body to the message when creating the exception but I'm not sure everyone would like that behavior (especially if it changes past behavior).

  2. Create a custom template but than you need to make sure to keep your template up-to-date when upgrading OpenAPI.

Additional context

ApiClient:

public class ApiClient {
  ...
  private ExceptionHandler exceptionHandler;
  ...

PostsApi:

public IncidentsApi(ApiClient apiClient) {
  ...
  private final ExceptionHandler exceptionHandler;
  ...
  public IncidentsApi(ApiClient apiClient) {
    ...
    memberVarExceptionHandler = apiClient.getExceptionHandler();
  }
  ...  
}

Default implementation:

public class DefaultExceptionHandler implements ExceptionHandler {

  @Override
  public ApiException createUnsuccessfulResponseException(String msg, HttpResponse<InputStream> response) {
    String bodyStr = null;
    try {
      bodyStr = new String(response.body().readAllBytes());
    } catch (Exception e) {
      bodyStr = "<Failed to read response body: " + e + ">";
    }

    return new ApiException(response.statusCode(), msg, response.headers(), bodyStr);
  }

  @Override
  public ApiException createException(Exception e) {
    return new ApiException(e);
  }

  @Override
  public ApiException createInvalidRequestException(int statusCode, String msg) {
    return  new ApiException(statusCode, msg);
  }
}

Usage example:

      if (localVarResponse.statusCode()/ 100 != 2) {
        throw exceptionHandler.createUnsuccessfulResponseException(
            "createPost call received non-success response",
            localVarResponse
        );
      }

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the Java ApiClient and generated xxxApi/PostsApi entry points described in the issue, tracing how unsuccessful responses and other exceptions are currently created. Define the handler integration while preserving the default behavior shown by DefaultExceptionHandler; done means generated client calls use the handler and custom handlers can replace the resulting exceptions.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
api
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.