@Slf4j
@RestControllerAdvice
public class ExceptionAdvice {
@ResponseBody
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
@ExceptionHandler(Exception.class)
public String validExceptionHandler(Exception e){
log.error("执行异常",e);
if (e instanceof MethodArgumentNotValidException){
MethodArgumentNotValidException exception= (MethodArgumentNotValidException) e;
return exception.getBindingResult().getFieldError().getDefaultMessage();
}
else if (e instanceof UnauthorizedException){
return "不具备相关权限";
}
else if (e instanceof EmosException){
EmosException exception= (EmosException) e;
return exception.getMsg();
}
else {
return "后端执行异常";
}
}
}
|