Handling System Exceptions

  1. Create the Handler package in the Common service

  2. Create a unified exception handler class in Handler

    @ControllerAdvice
    public class GlobalExceptionHandler {
    
        /** * Universal Exception handling method * defines a pointcut. During Exception handling, any Exception that is found to be of type Exception will be called by the method *@paramE Caught exceptions *@returnUnified response data */
        @ExceptionHandler(Exception.class)
        @ResponseBody
        public Result error(Exception e) {
            // Output abnormal stack information
            e.printStackTrace();
            return Result.error(ResultCodeEnum.UNKNOWN_REASON_ERROR);
        }
    
    / / -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- specific exception handling -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    / / -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - according to the needs, set -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
        
        @ExceptionHandler(BadSqlGrammarException.class)
        @ResponseBody
        public Result badSqlGrammarError(BadSqlGrammarException e) {
            e.printStackTrace();
            return Result.error(ResultCodeEnum.BAD_SQL_GRAMMAR_ERROR);
        }
    
        @ExceptionHandler(HttpMessageNotReadableException.class)
        @ResponseBody
        public Result jsonParseError(HttpMessageNotReadableException e) {
            e.printStackTrace();
            returnResult.error(ResultCodeEnum.JSON_PARSE_ERROR); }}Copy the code

Custom exception

  1. Create the Exception package in the Common service

  2. Create exception instances in the unified result enumeration class

    @Getter
    public enum ResultCodeEnum {
    
        // All enumerators must be defined first, terminated with a semicolon
        SUCCESS(true.20000."Success"),
        UNKNOWN_REASON_ERROR(false.20001."Unknown error"),
        BAD_SQL_GRAMMAR_ERROR(false.20002."SQL syntax error"),
        JSON_PARSE_ERROR(false.20003."Json parsing error"),
        // Custom exception instances
        CUSTOM_ERROR(false.21000."Custom exception");
    
        private final Boolean success;
        private final Integer code;
        private final String message;
    
        ResultCodeEnum(Boolean success, Integer code, String message) {
            this.success = success;
            this.code = code;
            this.message = message; }}Copy the code
  3. Create a custom exception class in Exception

    @Getter
    public class CustomException extends RuntimeException {
    
        /** * Initializes the custom exception *@paramResultCodeEnum User-defined exception instance */
        public CustomException(ResultCodeEnum resultCodeEnum) {
            super(resultCodeEnum.getMessage());
        }
    
        /** * override toString method *@returnReturn an exception message */
        @Override
        public String toString(a) {
            return "message:" + this.getMessage(); }}Copy the code

Handle custom exceptions

  1. Throw an exception in the right place

    throw new CustomException(ResultEnum.CUSTOM_ERROR);
    Copy the code
  2. Define handling methods in the unified exception handler class

    @ControllerAdvice
    public class GlobalExceptionHandler {
    
        /** * Universal Exception handling method * defines a pointcut. During Exception handling, any Exception that is found to be of type Exception will be called by the method *@paramE Exceptions to catch *@returnUnified response data */
        @ExceptionHandler(Exception.class)
        @ResponseBody
        public Result error(Exception e) {
            // Output abnormal stack information
            e.printStackTrace();
            return Result.error(ResultCodeEnum.UNKNOWN_REASON_ERROR);
        }
    
    / / -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- specific exception handling -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    / / -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - according to the needs, set -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
        
        @ExceptionHandler(BadSqlGrammarException.class)
        @ResponseBody
        public Result badSqlGrammarError(BadSqlGrammarException e) {
            e.printStackTrace();
            return Result.error(ResultCodeEnum.BAD_SQL_GRAMMAR_ERROR);
        }
    
        @ExceptionHandler(HttpMessageNotReadableException.class)
        @ResponseBody
        public Result jsonParseError(HttpMessageNotReadableException e) {
            e.printStackTrace();
            return Result.error(ResultCodeEnum.JSON_PARSE_ERROR);
        }
        
        // Custom exception handling methods
        @ExceptionHandler(CustomException.class)
        @ResponseBody
        public Result customError(CustomException e) {
            e.printStackTrace();
            returnResult.error(ResultCodeEnum.CUSTOM_ERROR); }}Copy the code