全局异常处理

@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());

    }

}

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容