Commit 038f3f1e by xuwenqian

feat:异常修改

parent 17752986
...@@ -3,6 +3,7 @@ package com.gic.plug.web.exception; ...@@ -3,6 +3,7 @@ package com.gic.plug.web.exception;
import com.gic.commons.exception.BaseRuntimeException; import com.gic.commons.exception.BaseRuntimeException;
import com.gic.commons.webapi.reponse.RestResponse; import com.gic.commons.webapi.reponse.RestResponse;
import com.gic.enterprise.exception.CommonException; import com.gic.enterprise.exception.CommonException;
import com.gic.goods.api.dto.ErrorCode;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
...@@ -12,7 +13,6 @@ import org.springframework.web.bind.MethodArgumentNotValidException; ...@@ -12,7 +13,6 @@ import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice; import org.springframework.web.bind.annotation.RestControllerAdvice;
import javax.servlet.http.HttpServletRequest;
import javax.validation.ConstraintViolation; import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException; import javax.validation.ConstraintViolationException;
import java.util.ArrayList; import java.util.ArrayList;
...@@ -31,15 +31,26 @@ public class SpringGlobalExceptionHandler { ...@@ -31,15 +31,26 @@ public class SpringGlobalExceptionHandler {
@ExceptionHandler(value = Exception.class) @ExceptionHandler(value = Exception.class)
public RestResponse handlerException(Exception exception) { public RestResponse handlerException(Exception e) {
String message = exception.getMessage(); log.error("系统异常:{}", e);
return RestResponse.failure(com.gic.goods.api.dto.ErrorCode.UNKNOW_ERROR.getCode(), com.gic.goods.api.dto.ErrorCode.UNKNOW_ERROR.getErrorMessage()); return RestResponse.failure(ErrorCode.UNKNOW_ERROR.getCode(), ErrorCode.UNKNOW_ERROR.getErrorMessage());
} }
@ExceptionHandler(value = NullPointerException.class) @ExceptionHandler(value = NullPointerException.class)
public RestResponse nullExceptionHandler(HttpServletRequest req, NullPointerException e) { public RestResponse nullExceptionHandler(NullPointerException e) {
log.error("发生空指针异常!原因是:", e); log.error("空指针异常!原因是:{}", e);
return RestResponse.failure(com.gic.goods.api.dto.ErrorCode.UNKNOW_ERROR.getCode(), e.getMessage()); return RestResponse.failure(ErrorCode.NULL_ERROR.getCode(), e.getMessage());
}
@ExceptionHandler(value = BaseRuntimeException.class)
public RestResponse baseRuntimeException(BaseRuntimeException e) {
log.error("运行异常!原因是:{}", e);
return RestResponse.failure(e.getErrCode(), e.getMessage());
}
@ExceptionHandler(value = CommonException.class)
public RestResponse enterpriseCommonException(CommonException e) {
return RestResponse.failure(e.getErrorCode(), e.getMessage());
} }
/** /**
...@@ -51,13 +62,13 @@ public class SpringGlobalExceptionHandler { ...@@ -51,13 +62,13 @@ public class SpringGlobalExceptionHandler {
@ExceptionHandler(BindException.class) @ExceptionHandler(BindException.class)
public RestResponse handleBindException(BindException ex) { public RestResponse handleBindException(BindException ex) {
FieldError fieldError = ex.getBindingResult().getFieldError(); FieldError fieldError = ex.getBindingResult().getFieldError();
return RestResponse.failure(com.gic.enterprise.error.ErrorCode.MISS_PARAMETER.getCode(), fieldError.getDefaultMessage()); return RestResponse.failure(ErrorCode.INVALIDATE_PARAM.getCode(), fieldError.getDefaultMessage());
} }
@ExceptionHandler(value = MethodArgumentNotValidException.class) @ExceptionHandler(value = MethodArgumentNotValidException.class)
public RestResponse handleNotValid(MethodArgumentNotValidException exception) { public RestResponse handleNotValid(MethodArgumentNotValidException exception) {
String message = exception.getBindingResult().getFieldError().getDefaultMessage(); String message = exception.getBindingResult().getFieldError().getDefaultMessage();
return RestResponse.failure(com.gic.goods.api.dto.ErrorCode.INVALIDATE_PARAM.getCode(), message); return RestResponse.failure(ErrorCode.INVALIDATE_PARAM.getCode(), message);
} }
@ExceptionHandler(value = ConstraintViolationException.class) @ExceptionHandler(value = ConstraintViolationException.class)
...@@ -65,7 +76,7 @@ public class SpringGlobalExceptionHandler { ...@@ -65,7 +76,7 @@ public class SpringGlobalExceptionHandler {
List<String> msg = new ArrayList<>(); List<String> msg = new ArrayList<>();
Set<ConstraintViolation<?>> constraintViolations = exception.getConstraintViolations(); Set<ConstraintViolation<?>> constraintViolations = exception.getConstraintViolations();
RestResponse result = new RestResponse(); RestResponse result = new RestResponse();
result.setCode(com.gic.goods.api.dto.ErrorCode.INVALIDATE_PARAM.getCode()); result.setCode(ErrorCode.INVALIDATE_PARAM.getCode());
for (ConstraintViolation<?> constraintViolation : constraintViolations) { for (ConstraintViolation<?> constraintViolation : constraintViolations) {
msg.add(constraintViolation.getMessage()); msg.add(constraintViolation.getMessage());
} }
...@@ -73,14 +84,4 @@ public class SpringGlobalExceptionHandler { ...@@ -73,14 +84,4 @@ public class SpringGlobalExceptionHandler {
return result; return result;
} }
@ExceptionHandler(value = CommonException.class)
public RestResponse enterpriseCommonException(CommonException exception) {
return RestResponse.failure(exception.getErrorCode(), exception.getMessage());
}
@ExceptionHandler(value = BaseRuntimeException.class)
public RestResponse baseRuntimeException(BaseRuntimeException exception) throws Exception {
return RestResponse.failure(exception.getErrCode(), exception.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