Skip to content

acidelk/error-handling

Repository files navigation

Spring boot starter for error handling in webflux microservices

Codacy Badge Build Status Maven Central

This starter handle all exceptions and log their.

What response model

{
    "error": {
        "code": -1,
        "message": "Something went wrong"
        "errorClass": "SomethingException",
        "serviceName": "my-best-api"
    }
}

How to use

build.gradle

dependencies {
    implementation 'com.github.acidelk:error-handling:{version}'
}

How can i customise handlers for my exceptions

@Configuration
public class ErrorHandlerConfiguration {

  @Bean
  public DefaultExceptionConverter defaultExceptionConverter(
      @Value("${spring.application.name}") String application
  ) {
    return new DefaultExceptionConverter(application)
        .withHandler(HttpUnknownException.class, this::handleUnknownException)
        .withDefaultHandler(this::handleDefaultException);
  }

  private ErrorResponse handleDefaultException(Exception e) {
    return new ErrorResponse(HttpStatus.I_AM_A_TEAPOT, -1, e.getMessage(), e.getClass().getSimpleName());
  }

  private ErrorResponse handleUnknownException(Exception e) {
    return ((HttpUnknownException) e).getErrorResponse();
  }
}

Create simple exception using ServiceException

@Getter
@ToString
public class DifferentChecksumException extends ServiceException {

  public DifferentChecksumException(String msg) {
    super(HttpStatus.NOT_FOUND, 100, msg);
  }

}

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages