|
|
@ -4,8 +4,10 @@ import com.iflytop.handacid.common.exception.AppException; |
|
|
|
import com.iflytop.handacid.common.result.Result; |
|
|
|
import com.iflytop.handacid.common.result.ResultCode; |
|
|
|
import jakarta.servlet.http.HttpServletRequest; |
|
|
|
import jakarta.validation.ConstraintViolation; |
|
|
|
import jakarta.validation.ConstraintViolationException; |
|
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
|
import org.springframework.context.support.DefaultMessageSourceResolvable; |
|
|
|
import org.springframework.validation.BindException; |
|
|
|
import org.springframework.web.HttpRequestMethodNotSupportedException; |
|
|
|
import org.springframework.web.bind.MethodArgumentNotValidException; |
|
|
@ -36,7 +38,7 @@ public class GlobalExceptionHandler { |
|
|
|
@ExceptionHandler(MethodArgumentNotValidException.class) |
|
|
|
public Result<?> handleBodyValid(MethodArgumentNotValidException ex) { |
|
|
|
String msg = ex.getBindingResult().getFieldErrors().stream() |
|
|
|
.map(f -> f.getField() + ": " + f.getDefaultMessage()) |
|
|
|
.map(DefaultMessageSourceResolvable::getDefaultMessage) |
|
|
|
.collect(Collectors.joining("; ")); |
|
|
|
return Result.failed(ResultCode.INVALID_PARAMETER.getCode(), msg); |
|
|
|
} |
|
|
@ -45,11 +47,7 @@ public class GlobalExceptionHandler { |
|
|
|
@ExceptionHandler(ConstraintViolationException.class) |
|
|
|
public Result<?> handleParamValid(ConstraintViolationException ex) { |
|
|
|
String msg = ex.getConstraintViolations().stream() |
|
|
|
.map(v -> { |
|
|
|
String path = v.getPropertyPath().toString(); |
|
|
|
String field = path.substring(path.lastIndexOf('.') + 1); |
|
|
|
return field + ": " + v.getMessage(); |
|
|
|
}) |
|
|
|
.map(ConstraintViolation::getMessage) |
|
|
|
.collect(Collectors.joining("; ")); |
|
|
|
return Result.failed(ResultCode.INVALID_PARAMETER.getCode(), msg); |
|
|
|
} |
|
|
@ -58,7 +56,7 @@ public class GlobalExceptionHandler { |
|
|
|
@ExceptionHandler(BindException.class) |
|
|
|
public Result<?> handleBind(BindException ex) { |
|
|
|
String msg = ex.getFieldErrors().stream() |
|
|
|
.map(f -> f.getField() + ": " + f.getDefaultMessage()) |
|
|
|
.map(DefaultMessageSourceResolvable::getDefaultMessage) |
|
|
|
.collect(Collectors.joining("; ")); |
|
|
|
return Result.failed(ResultCode.INVALID_PARAMETER.getCode(), msg); |
|
|
|
} |
|
|
|