“This is the fourth day of my participation in the Gwen Challenge.

Battery anxiety:

For the exception, the test of life is also facing the uncertainty of life, such as when we phone quickly without electricity, we will be anxious to have it power, prevent the shutdown affects our work and life experience, a product or project in the actual operation, then the change of environment and context, the problem also is varied, such as I build a car, I want to consider, High temperature, the temperature of the car, to help heat dissipation, prevent the occurrence of safety accidents, or the use of alarm, this is a way to treat abnormal processing.

If we can’t solve it, we will throw it out. This is also the first sentence the teacher gave us before I learned abnormality. The second sentence is that we have changed it.

abnormal

Exceptions refer to exceptions that occur during the compilation or execution of the project, such as parameter conversion, type exception, array out of bounds, null pointer and many other exceptions.

The version used this time is springBoot 2.2.13GA version

Springboot official document address

Found in springBoot Features


abnormal

ErrorHandler file address for SpringBoot

Error handling

Springboot has error mechanisms by default, which are divided into two types of global exception handling, custom exception handling;

Global exception handling:

In fact, when designing the framework, we have already configured the exception problem during operation,

Global exception handling principle:

We use @ControllerAdvice, which means that we have defined a controller enhancement class that will intercept the current request when any other controller sends an exception and serves the exception class specified in the @ExceptionHandler annotation

In essence: controller enhancer capture, specific exception interception do match

Create a unified exception handling class

GlobalExceptionHandler


The test class:

@RestControllerAdvice

public class GlobalExceptionHandler {



/ * *

Parameter parsing failed

* /

    @ResponseStatus(HttpStatus.OK)

    @ExceptionHandler(HttpMessageNotReadableException.class)

    public Result handleHttpMessageNotReadableException(HttpMessageNotReadableException ex) {

        log.error("Parameter resolution failed", ex);

        return Result.failCode202("Parameter resolution failed");

    }



/ * *

* Parameter verification is abnormal

* /

    @ResponseStatus(HttpStatus.OK)

    @ExceptionHandler(MethodArgumentNotValidException.class)

    public Result handleMethodArgumentNotValidException(MethodArgumentNotValidException ex) {

        log.error("Parameter verification exception", ex);

        BindingResult result = ex.getBindingResult();

        FieldError error = result.getFieldError();

        String field = error.getField();

        String code = error.getDefaultMessage();

        String message = String.format("%s:%s", field, code);

        return Result.failCode202("Parameter verification exception :" + message);

    }



/ * *

* Parameter verification is abnormal

* /

    @ResponseStatus(HttpStatus.OK)

    @ExceptionHandler(ValidationException.class)

    public Result handleValidationException(ValidationException ex) {

        log.error("Parameter verification exception", ex);

        return Result.failCode202("Parameter verification exception");

    }



/ * *

* The current request method is not supported

* /

    @ResponseStatus(HttpStatus.OK)

    @ExceptionHandler(HttpRequestMethodNotSupportedException.class)

    public Result handleHttpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException ex) {

        log.error("Current requested method not supported", ex);

        return Result.failCode202("Current requested method not supported");

    }





   

    @ResponseStatus(HttpStatus.OK)

    @ExceptionHandler(UnauthorizedException.class)

    public Result handle401() {

        return Result.failCode403("Insufficient authority");

    }



/ * *

* Service processing is abnormal

* /

    @ResponseStatus(HttpStatus.OK)

    @ExceptionHandler(TestException.class)

    public Result handleTestException(TestException ex) {

        log.error("Service Processing exception", ex);

        return Result.failCode202(ex.getMessage());

    }





/ * *

* Catch all other exceptions

* /

    @ExceptionHandler(Exception.class)

    @ResponseStatus(HttpStatus.OK)

    public Result globalException(Throwable ex) {

        log.error("Exception information", ex);

        return Result.fail(HttpConstants.STATUS_500, "System exception");

    }



}



Copy the code

conclusion

For the global exception, according to the annotation enhanced by the exception controller, and then distinguish the exception type according to the customization, so as to achieve the matching trigger customized exception processing, which also needs to pay attention to, about

  • The @ExceptionHandler annotation has only one value parameter, which is the specified exception class;
@ExceptionHandler(MyException.class)

Copy the code
  • Looking at the source parameter, we can also specify the package path of the controller we want to intercept.

So that’s it. I’m looking forward to the next time we try to define SpringBoot exceptions using @AbstracTerrorController, the underlying class


I’m Luca and it’s Apple’s developer conference tonight, so I have to check out the latest possible release of M1X, so be sure to like it before you go.