Commit 94ad1af3 by tyfanchz

1、功能开发

2、细化统一异常处理方法
3、去除无用校验
1 parent 7723a017
...@@ -29,7 +29,7 @@ public class ProductTypeController { ...@@ -29,7 +29,7 @@ public class ProductTypeController {
* @return 结果 * @return 结果
*/ */
@RequestMapping(value = "/getTypeAndCategory", method = {RequestMethod.GET, RequestMethod.POST}) @RequestMapping(value = "/getTypeAndCategory", method = {RequestMethod.GET, RequestMethod.POST})
public R getProductTypeTree() { public R getTypeAndCategory() {
log.info("$$$$ /productType/getTypeAndCategory..."); log.info("$$$$ /productType/getTypeAndCategory...");
JSONObject result = productTypeFeignClient.getTypeAndCategory(); JSONObject result = productTypeFeignClient.getTypeAndCategory();
return R.ok().data(result); return R.ok().data(result);
......
package com.glsx.api.exception; package com.glsx.api.exception;
import java.util.List;
import java.util.concurrent.TimeoutException;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.glsx.plat.common.utils.StringUtils; import com.glsx.plat.common.utils.StringUtils;
...@@ -8,6 +10,7 @@ import com.netflix.hystrix.exception.HystrixRuntimeException; ...@@ -8,6 +10,7 @@ import com.netflix.hystrix.exception.HystrixRuntimeException;
import feign.FeignException; import feign.FeignException;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindException; import org.springframework.validation.BindException;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException; import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.ServletRequestBindingException; import org.springframework.web.bind.ServletRequestBindingException;
import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.ExceptionHandler;
...@@ -33,8 +36,27 @@ public class GlobalExceptionHandler { ...@@ -33,8 +36,27 @@ public class GlobalExceptionHandler {
BindException.class}) BindException.class})
public R handleBindException(Exception e) { public R handleBindException(Exception e) {
log.error("!!!! 参数错误", e); log.error("!!!! 参数错误", e);
if ((e instanceof MethodArgumentNotValidException) || (e instanceof BindException)) {
List<FieldError> errorList;
if (e instanceof MethodArgumentNotValidException) {
MethodArgumentNotValidException ex = (MethodArgumentNotValidException) e;
errorList = ex.getBindingResult().getFieldErrors();
} else {
BindException ex = (BindException) e;
errorList = ex.getFieldErrors();
}
StringBuffer messageSb = new StringBuffer();
for (FieldError error : errorList) {
String fieldMessage = String.format("%s: %s, ",
error.getField(), error.getDefaultMessage());
messageSb.append(fieldMessage);
}
return R.error(ResultEnum.PARAM_ERROR.getCode(),
messageSb.toString().replaceAll(", +$", ""));
} else {
return R.error(ResultEnum.PARAM_ERROR.getCode(), ResultEnum.PARAM_ERROR.getMessage()); return R.error(ResultEnum.PARAM_ERROR.getCode(), ResultEnum.PARAM_ERROR.getMessage());
} }
}
@ExceptionHandler({HystrixRuntimeException.class}) @ExceptionHandler({HystrixRuntimeException.class})
public R handleHystrixException(HystrixRuntimeException e) { public R handleHystrixException(HystrixRuntimeException e) {
...@@ -42,6 +64,10 @@ public class GlobalExceptionHandler { ...@@ -42,6 +64,10 @@ public class GlobalExceptionHandler {
if (cause instanceof FeignException) { if (cause instanceof FeignException) {
FeignException feignException = (FeignException) cause; FeignException feignException = (FeignException) cause;
return this.handleFeignException(feignException); return this.handleFeignException(feignException);
} else if (cause instanceof TimeoutException) {
log.error("!!!! 服务调用超时", e);
return R.error(ResultEnum.REMOTE_SERVER_TIME_OUT.getCode(),
ResultEnum.REMOTE_SERVER_TIME_OUT.getMessage());
} else { } else {
log.error("!!!! Hystrix熔断错误", e); log.error("!!!! Hystrix熔断错误", e);
return R.error(ResultEnum.ERROR.getCode(), ResultEnum.ERROR.getMessage()); return R.error(ResultEnum.ERROR.getCode(), ResultEnum.ERROR.getMessage());
......
...@@ -19,6 +19,7 @@ public enum ResultEnum { ...@@ -19,6 +19,7 @@ public enum ResultEnum {
PRODUCT_TYPE_NOT_FOUND(3001, "产品类型ID未找到"), PRODUCT_TYPE_NOT_FOUND(3001, "产品类型ID未找到"),
REMOTE_SERVICE_ERROR(9001, "服务调用错误"), REMOTE_SERVICE_ERROR(9001, "服务调用错误"),
REMOTE_SERVER_TIME_OUT(9002, "远程服务超时,请重试"),
; ;
private final Integer code; private final Integer code;
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!