@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {
? ? @ResponseStatus(HttpStatus.BAD_REQUEST)
? ? @ExceptionHandler(value = RuntimeException.class)
? ? public Result hadler(RuntimeException e){
? ? ? ? log.error("運(yùn)行時(shí)異常:-------------{}",e);
? ? return Result.fail(e.getMessage());
? ? }
//? ? @ResponseStatus(HttpStatus.UNAUTHORIZED)
//? ? @ExceptionHandler(value = ShiroException.class)
//? ? public Result hadler(ShiroException e){
//? ? ? ? log.error("運(yùn)行時(shí)異常:-------------{}",e);
//? ? ? ? return Result.fail(401,e.getMessage(),null);
//? ? }
? ? @ResponseStatus(HttpStatus.BAD_REQUEST)
? ? @ExceptionHandler(value = MethodArgumentNotValidException.class)
? ? public Result hadler(MethodArgumentNotValidException e){
? ? ? ? log.error("實(shí)體校驗(yàn)異常:-------------{}",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());
? ? }
}