Skip to content

Customising error response format #517

@headly48

Description

@headly48

Expected Behavior

Currently upgrading and running into an issue with applying custom formatting to error responses.

Prior to version 4.6.0 the ErrorResponseProcessor could be used to return a custom error response body for all errors however after upgrading to 4.6.0 the custom error formating is no longer being applied

Actual Behaviour

After upgrading to 4.6.0 the ErrorResponseProcessor is no longer called.

Steps To Reproduce

  package example.micronaut;
  
  import io.micronaut.context.annotation.Primary;
  import io.micronaut.core.annotation.NonNull;
  import io.micronaut.http.HttpMethod;
  import io.micronaut.http.HttpStatus;
  import io.micronaut.http.MediaType;
  import io.micronaut.http.MutableHttpResponse;
  import io.micronaut.http.server.exceptions.response.Error;
  import io.micronaut.http.server.exceptions.response.ErrorContext;
  import io.micronaut.http.server.exceptions.response.ErrorResponseProcessor;
  import io.micronaut.serde.annotation.Serdeable;
  import jakarta.inject.Singleton;
  
  import java.util.HashSet;
  import java.util.Set;
  import java.util.UUID;
  
  @Singleton
  @Primary
  public class CustomErrorResponseProcessor<E> implements ErrorResponseProcessor<CustomErrorResponseProcessor.Errors> {
  
      @Serdeable.Serializable
      public record Errors (Set<ErrorMessage> errors) {
  
      }
  
      @Serdeable.Serializable
      public record ErrorMessage (String id, String code, String details) {
  
      }
  
      @Override
      public @NonNull MutableHttpResponse<Errors> processResponse(@NonNull ErrorContext errorContext, @NonNull MutableHttpResponse<?> response) {
  
          if (errorContext.getRequest().getMethod() == HttpMethod.HEAD) {
              return (MutableHttpResponse<Errors>) response;
          }
  
          Set<ErrorMessage> errorMessages = new HashSet<>();
          if (!errorContext.hasErrors()) {
              ErrorMessage errorMessage = new ErrorMessage(getUUID(), HttpStatus.valueOf(response.code()).name(), response.reason());
              
              errorMessages.add(errorMessage);
          } else {
              for (Error e : errorContext.getErrors()) {
                  ErrorMessage errorMessage = new ErrorMessage(getUUID(), HttpStatus.valueOf(response.code()).name(), e.getMessage());
  
                  errorMessages.add(errorMessage);
              }
          }
          Errors errors = new Errors(errorMessages);
          return response.body(errors).contentType(MediaType.APPLICATION_JSON_TYPE);
      }
  
  
      private String getUUID() {
          return UUID.randomUUID().toString();
      }
  }

When making a request to an endpoint that does not exist with version=4.5.0 the response body is

{
    "errors": [
        {
            "id": "b6c34008-6dca-449f-b341-655a5bd0d0e6",
            "code": "NOT_FOUND",
            "details": "Page Not Found"
        }
    ]
}

with version=4.6.0 the response body is empty

Environment Information

No response

Example Application

No response

Version

=4.6.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions