mirror of
https://gitee.com/czh-dev/upload-hub
synced 2026-05-10 17:43:25 +08:00
28 lines
1.1 KiB
Java
28 lines
1.1 KiB
Java
package cn.czh.advice;
|
|
|
|
import cn.czh.base.BusinessException;
|
|
import cn.czh.base.Result;
|
|
import org.springframework.http.HttpStatus;
|
|
import org.springframework.validation.FieldError;
|
|
import org.springframework.web.bind.MethodArgumentNotValidException;
|
|
import org.springframework.web.bind.annotation.ExceptionHandler;
|
|
import org.springframework.web.bind.annotation.ResponseStatus;
|
|
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
|
|
|
@RestControllerAdvice
|
|
public class GlobalExceptionHandler {
|
|
|
|
@ExceptionHandler(MethodArgumentNotValidException.class)
|
|
@ResponseStatus(HttpStatus.BAD_REQUEST)
|
|
public Result<?> handleValidationException(MethodArgumentNotValidException ex) {
|
|
FieldError fieldError = ex.getBindingResult().getFieldErrors().get(0);
|
|
String errorMessage = fieldError.getDefaultMessage();
|
|
return Result.error(400, errorMessage);
|
|
}
|
|
|
|
@ExceptionHandler(BusinessException.class)
|
|
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
|
|
public Result<?> handleBusinessException(BusinessException e) {
|
|
return Result.error(500, e.getMessage());
|
|
}
|
|
} |