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

This commit is contained in:
有来技术 2021-10-20 00:26:56 +08:00
parent 20ee7edd63
commit e4f81463d8

View File

@ -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 {
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 {
@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();
} }
/** /**