Commit 0fd3170a by 陶光胜

异常捕获

parent 67734aa1
......@@ -28,30 +28,10 @@ import com.gic.enterprise.error.ErrorCode;
public class GlobalExceptionHandler {
private static Logger logger = LogManager.getLogger(GlobalExceptionHandler.class);
/**
* 参数校验异常统一处理
* @param e
* @return
*/
@ExceptionHandler(BindException.class)
public RestResponse customException(BindException e) {
List<FieldError> fieldErrors = e.getFieldErrors();
StringBuilder errorMessage = new StringBuilder();
fieldErrors.forEach(fieldError -> {
errorMessage
.append(fieldError.getDefaultMessage())
.append(",");
});
String error = errorMessage.toString();
return getRestResponse(ErrorCode.PARAMETER_ERROR.getCode(), error.substring(0, error.length() - 1));
}
@ExceptionHandler(ConstraintViolationException.class)
public RestResponse constraintViolationException(ConstraintViolationException e) {
Set<ConstraintViolation<?>> constraintViolations = e.getConstraintViolations();
String paramName = constraintViolations.iterator().next().getPropertyPath().toString();
String paramError = constraintViolations.iterator().next().getMessage();
return getRestResponse(ErrorCode.PARAMETER_ERROR.getCode(), getFailFastMsg(paramName, paramError));
@ExceptionHandler(RuntimeException.class)
public RestResponse runtimeException(RuntimeException e){
return RestResponse.failure(ErrorCode.SYSTEM_ERROR.getCode(), "系统异常,请联系管理员", e.getMessage());
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment