提交 e4f81463 编写于 作者: 有来技术

fix(GlobalExceptionHandler.class): 微服务Feign调用全局异常处理

上级 20ee7edd
...@@ -4,6 +4,7 @@ import cn.hutool.json.JSONObject; ...@@ -4,6 +4,7 @@ import cn.hutool.json.JSONObject;
import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.JsonProcessingException;
import com.youlai.common.result.Result; import com.youlai.common.result.Result;
import com.youlai.common.result.ResultCode; import com.youlai.common.result.ResultCode;
import feign.FeignException;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.TypeMismatchException; import org.springframework.beans.TypeMismatchException;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
...@@ -159,19 +160,23 @@ public class GlobalExceptionHandler { ...@@ -159,19 +160,23 @@ public class GlobalExceptionHandler {
return Result.failed(e.getMessage()); return Result.failed(e.getMessage());
} }
/**
* CompletionException
*/
@ResponseStatus(HttpStatus.BAD_REQUEST) @ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(CompletionException.class) @ExceptionHandler(CompletionException.class)
public <T> Result<T> processException(CompletionException e) { public <T> Result<T> processException(CompletionException e) {
log.error(e.getMessage(), e);
if (e.getMessage().startsWith("feign.FeignException")) { if (e.getMessage().startsWith("feign.FeignException")) {
return Result.failed("微服务调用异常"); return Result.failed("微服务调用异常");
} }
return handleException(e); return handleException(e);
} }
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(FeignException.BadRequest.class)
public <T> Result<T> processException(FeignException.BadRequest e) {
log.info("微服务feign调用异常:{}", e.getMessage());
return Result.failed(e.getMessage());
}
@ResponseStatus(HttpStatus.BAD_REQUEST) @ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(BizException.class) @ExceptionHandler(BizException.class)
public <T> Result<T> handleBizException(BizException e) { public <T> Result<T> handleBizException(BizException e) {
...@@ -185,8 +190,7 @@ public class GlobalExceptionHandler { ...@@ -185,8 +190,7 @@ public class GlobalExceptionHandler {
@ResponseStatus(HttpStatus.BAD_REQUEST) @ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(Exception.class) @ExceptionHandler(Exception.class)
public <T> Result<T> handleException(Exception e) { public <T> Result<T> handleException(Exception e) {
log.error("未知异常,异常原因:{}", e.getMessage(), e); return Result.failed(e.getLocalizedMessage());
return Result.failed();
} }
/** /**
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册