error-handling-spring-boot-starter icon indicating copy to clipboard operation
error-handling-spring-boot-starter copied to clipboard

How to handle UnsupportedMediaTypeException with custom message ?

Open PostIt59 opened this issue 2 years ago • 4 comments

Hello @wimdeblauwe,

First of all, thanks for this amazing project ;)

I maintain an API with a PATCH endpoint with require application/merge-patch+json content-type. Since, it's not a common header I need to handle properly UnsupportedMediaTypeException. Without any custom configuration, the library returns a 415 Http Status which is great. But, no body is returned. Can you explain how can I customize the response body for an error which is not custom but part of the spring project.

Thanks for your help

PostIt59 avatar Sep 01 '22 09:09 PostIt59

Back,

I've tried to add a custom configuration as the docs explains :

error:
  handling:
    codes:
      org.springframework.web.HttpMediaTypeNotSupportedException: "INVALID_MEDIA_TYPE"
    messages:
      org.springframework.web.HttpMediaTypeNotSupportedException: "Wrong media type provided"

But it still doesn't work :(

Also, I have another question. Is this project still active ?

Thanks for your reply

PostIt59 avatar Sep 09 '22 07:09 PostIt59

It is active, but there is currently little activity as it is quite stable as is. Do you have a sample project that shows what you want to do so I can investigate it?

wimdeblauwe avatar Sep 09 '22 08:09 wimdeblauwe

I just created a sample project myself, but it seems the exception never reaches the library for some reason. In a very simple Spring Boot project even without my library I see that an @ExceptionHandler is not called.

If you have this:

@RestController
public class TestRestController {

    @ExceptionHandler
    public void handleException(Exception e) {
        System.out.println("Exception!");
        e.printStackTrace();
    }

        
   @GetMapping("/")
    public void test() {
        throw new RuntimeException("fake");
    }
}

Then the handler works and the stack trace is printed. However, If you change the GetMapping to:

@GetMapping(value = "/", consumes = "application/merge-patch+json")

Then the stack trace is no longer printed. I will try to check with Spring Boot people why this might be.

wimdeblauwe avatar Sep 09 '22 11:09 wimdeblauwe

What you can try as a workaround for now, is defining a class like this in your project:

import io.github.wimdeblauwe.errorhandlingspringbootstarter.servlet.ErrorHandlingControllerAdvice;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.web.bind.annotation.ControllerAdvice;

import java.util.List;

@ControllerAdvice
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET)
public class FallbackExceptionHandler extends ErrorHandlingControllerAdvice {

    public FallbackExceptionHandler(List<ApiExceptionHandler> handlers,
                                    FallbackApiExceptionHandler fallbackHandler,
                                    LoggingService loggingService) {
        super(handlers, fallbackHandler, loggingService);
    }
}

wimdeblauwe avatar Sep 13 '22 14:09 wimdeblauwe

I tested this in a sample application with your config to override the error message and this works perfectly.

wimdeblauwe avatar Sep 19 '22 06:09 wimdeblauwe