Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

In REST API, improve exception handling #4824

Closed
2 tasks done
yanavasileva opened this issue Nov 29, 2024 · 0 comments
Closed
2 tasks done

In REST API, improve exception handling #4824

yanavasileva opened this issue Nov 29, 2024 · 0 comments
Assignees
Labels
scope:core-api Changes to the core API: engine, dmn-engine, feel-engine, REST API, OpenAPI type:task Issues that are a change to the project that is neither a feature nor a bug fix. version:7.23.0-alpha2 version:7.23.0

Comments

@yanavasileva
Copy link
Member

yanavasileva commented Nov 29, 2024

Created on behalf of #4600

Context

While implementing custom exception handling for my Camunda REST API, I used the ExceptionHandlerHelper class from the engine-rest module to ensure that the behavior of error handling is consistent with the built-in Camunda REST API error handling. The custom exception handler code is as follows:

@ControllerAdvice
public class CustomExceptionHandler extends ResponseEntityExceptionHandler {

    protected static final ExceptionLogger LOGGER = ExceptionLogger.REST_LOGGER;
    private static final ExceptionHandlerHelper DELEGATE = ExceptionHandlerHelper.getInstance();

    @ExceptionHandler
    public ResponseEntity<Object> handleAnyException(Throwable ex, NativeWebRequest request) {
         ExceptionDto exceptionDto = DELEGATE.fromException(ex);
         return handleExceptionInternal((Exception) ex, exceptionDto, buildHeaders(ex), HttpStatusCode.valueOf(DELEGATE.getStatus(ex).getStatusCode()), request);
    }

    @Nullable
    @Override
    protected ResponseEntity<Object> handleExceptionInternal(
            Exception ex,
            @Nullable Object body,
            HttpHeaders headers,
            HttpStatusCode statusCode,
            WebRequest request
    ) {
        LOGGER.log(ex);
        if (body == null) {
            return super.handleExceptionInternal(ex, DELEGATE.fromException(ex), headers, statusCode, request);
        }
        return super.handleExceptionInternal(ex, body, headers, statusCode, request);
    }

    private HttpHeaders buildHeaders(Throwable err) {
        // ...
    }

}

However, the code field value in the response body (ExceptionDto) was always null, resulting in outputs like the following:

{
    "type": "AuthorizationException",
    "message": "The user with id 'tom' does not have 'CREATE' permission on resource 'Deployment'.",
    "code": null,
    "userId": "tom",
    "resourceName": "Deployment",
    "resourceId": null,
    "permissionName": "CREATE",
    "missingAuthorizations": [
        {
            "permissionName": "CREATE",
            "resourceName": "Deployment",
            "resourceId": null
        }
    ]
}

Acceptance Criteria (Required on creation)

  • Enable easier inheritance of ExceptionHandlerHelper by providing exception code already in org.camunda.bpm.engine.rest.exception.ExceptionHandlerHelper.fromException(Throwable)

Hints

Links

Breakdown

Pull Requests

Preview Give feedback
  1. yhao3
  2. ci:all-as ci:rest-api ci:webapp-integration
    yanavasileva

Dev2QA handover

  • Does this ticket need a QA test and the testing goals are not clear from the description? Add a Dev2QA handover comment
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
scope:core-api Changes to the core API: engine, dmn-engine, feel-engine, REST API, OpenAPI type:task Issues that are a change to the project that is neither a feature nor a bug fix. version:7.23.0-alpha2 version:7.23.0
Projects
None yet
Development

No branches or pull requests

2 participants