全局异常处理

@Slf4j

@RestControllerAdvice

public class GlobalExceptionHandler {

    @ResponseStatus(HttpStatus.BAD_REQUEST)

    @ExceptionHandler(value = RuntimeException.class)

    public Result hadler(RuntimeException e){

        log.error("运行时异常:-------------{}",e);

    return Result.fail(e.getMessage());

    }

//    @ResponseStatus(HttpStatus.UNAUTHORIZED)

//    @ExceptionHandler(value = ShiroException.class)

//    public Result hadler(ShiroException e){

//        log.error("运行时异常:-------------{}",e);

//        return Result.fail(401,e.getMessage(),null);

//    }

    @ResponseStatus(HttpStatus.BAD_REQUEST)

    @ExceptionHandler(value = MethodArgumentNotValidException.class)

    public Result hadler(MethodArgumentNotValidException e){

        log.error("实体校验异常:-------------{}",e);

        BindingResult bindingResult = e.getBindingResult();

        ObjectError objectError = bindingResult.getAllErrors().stream().findFirst().get();

        return Result.fail(objectError.getDefaultMessage());

    }

    @ResponseStatus(HttpStatus.BAD_REQUEST)

    @ExceptionHandler(value = IllegalArgumentException.class)

    public Result hadler(IllegalArgumentException e){

        log.error("Assert异常:-------------{}",e);

        return Result.fail(e.getMessage());

    }

}

你可能感兴趣的:(全局异常处理)