✨ Introducing new features. feign 异常直接抛出 方便 全局异常处理
This commit is contained in:
1 parent
f8734c9614
commit
cf285407f5
2 files changed
+22
-9
No files matched your search
+1
-9
@@ -8,7 +8,6 @@ import com.alibaba.csp.sentinel.SphU;
|
||||
import com.alibaba.csp.sentinel.Tracer;
|
||||
import com.alibaba.csp.sentinel.context.ContextUtil;
|
||||
import com.alibaba.csp.sentinel.slots.block.BlockException;
|
||||
import com.pig4cloud.pigx.common.core.util.R;
|
||||
import feign.Feign;
|
||||
import feign.InvocationHandlerFactory;
|
||||
import feign.MethodMetadata;
|
||||
@@ -120,14 +119,7 @@ public class PigxSentinelInvocationHandler implements InvocationHandler {
|
||||
}
|
||||
}
|
||||
else {
|
||||
// 若是R类型 执行自动降级返回R
|
||||
if (R.class == method.getReturnType()) {
|
||||
log.error("feign 服务间调用异常", ex);
|
||||
return R.failed(ex.getLocalizedMessage());
|
||||
}
|
||||
else {
|
||||
throw ex;
|
||||
}
|
||||
throw ex;
|
||||
}
|
||||
}
|
||||
finally {
|
||||
|
||||
+21
@@ -18,7 +18,10 @@
|
||||
package com.pig4cloud.pigx.common.sentinel.handle;
|
||||
|
||||
import com.alibaba.csp.sentinel.Tracer;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.pig4cloud.pigx.common.core.util.R;
|
||||
import feign.FeignException;
|
||||
import lombok.SneakyThrows;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
|
||||
import org.springframework.http.HttpStatus;
|
||||
@@ -43,6 +46,8 @@ import java.util.List;
|
||||
@ConditionalOnExpression("!'${security.oauth2.client.clientId}'.isEmpty()")
|
||||
public class GlobalBizExceptionHandler {
|
||||
|
||||
private final ObjectMapper objectMapper = new ObjectMapper();
|
||||
|
||||
/**
|
||||
* 全局异常.
|
||||
* @param e the e
|
||||
@@ -58,6 +63,22 @@ public class GlobalBizExceptionHandler {
|
||||
return R.failed(e.getLocalizedMessage());
|
||||
}
|
||||
|
||||
@SneakyThrows
|
||||
@ExceptionHandler(FeignException.class)
|
||||
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
|
||||
public R handleGlobalException(FeignException e) {
|
||||
log.error("全局异常信息 ex={}", e.getMessage(), e);
|
||||
|
||||
// 业务异常交由 sentinel 记录
|
||||
Tracer.trace(e);
|
||||
|
||||
if (e.responseBody().isPresent()) {
|
||||
return objectMapper.readValue(e.responseBody().get().array(), R.class);
|
||||
}
|
||||
|
||||
return R.failed(e.getLocalizedMessage());
|
||||
}
|
||||
|
||||
/**
|
||||
* AccessDeniedException
|
||||
* @param e the e
|
||||
|
||||
Reference in new issue
Block a user