feat: 完善通过 X-Metadata-Tag 进行异常记录
This commit is contained in:
parent
b48e60e337
commit
52401ef2f4
@ -0,0 +1,46 @@
|
|||||||
|
package cn.axzo.framework.autoconfigure.feign;
|
||||||
|
|
||||||
|
import cn.axzo.framework.core.RecordException;
|
||||||
|
import feign.Feign;
|
||||||
|
import feign.Response;
|
||||||
|
import feign.Util;
|
||||||
|
import feign.codec.ErrorDecoder;
|
||||||
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||||
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.web.servlet.DispatcherServlet;
|
||||||
|
|
||||||
|
import javax.servlet.Servlet;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import static java.nio.charset.StandardCharsets.UTF_8;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 为兼容 X-Metadata-Tag 的异常解码器
|
||||||
|
*
|
||||||
|
* @author wangli
|
||||||
|
* @since 2024/6/25 21:55
|
||||||
|
*/
|
||||||
|
@Configuration(proxyBeanMethods = false)
|
||||||
|
@ConditionalOnWebApplication
|
||||||
|
@ConditionalOnClass({Feign.class, Servlet.class, DispatcherServlet.class})
|
||||||
|
public class FeignErrorDecoderAutoConfiguration {
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public ErrorDecoder feignErrorDecoder() {
|
||||||
|
return (methodKey, response) -> {
|
||||||
|
if (response.status() != 512) {
|
||||||
|
return new ErrorDecoder.Default().decode(methodKey, response);
|
||||||
|
}
|
||||||
|
byte[] body = {};
|
||||||
|
try {
|
||||||
|
if (response.body() != null) {
|
||||||
|
body = Util.toByteArray(response.body().asInputStream());
|
||||||
|
}
|
||||||
|
} catch (IOException ignored) { // NOPMD
|
||||||
|
}
|
||||||
|
return new RecordException(new String(body, UTF_8));
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -3,12 +3,15 @@ package cn.axzo.framework.autoconfigure.web.exception.support;
|
|||||||
import cn.axzo.framework.autoconfigure.web.exception.handler.ExceptionResultHandler;
|
import cn.axzo.framework.autoconfigure.web.exception.handler.ExceptionResultHandler;
|
||||||
import cn.axzo.framework.autoconfigure.web.exception.handler.internal.StandardExceptionResultHandler;
|
import cn.axzo.framework.autoconfigure.web.exception.handler.internal.StandardExceptionResultHandler;
|
||||||
import cn.axzo.framework.core.InternalException;
|
import cn.axzo.framework.core.InternalException;
|
||||||
|
import cn.axzo.framework.core.RecordException;
|
||||||
import cn.axzo.framework.core.util.ClassUtil;
|
import cn.axzo.framework.core.util.ClassUtil;
|
||||||
import cn.axzo.framework.domain.web.code.BaseCode;
|
import cn.axzo.framework.domain.web.code.BaseCode;
|
||||||
import cn.axzo.framework.domain.web.result.ApiResult;
|
import cn.axzo.framework.domain.web.result.ApiResult;
|
||||||
import cn.axzo.framework.domain.web.result.Result;
|
import cn.axzo.framework.domain.web.result.Result;
|
||||||
import com.fasterxml.jackson.core.JsonParseException;
|
import com.fasterxml.jackson.core.JsonParseException;
|
||||||
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||||
import com.fasterxml.jackson.databind.JsonMappingException;
|
import com.fasterxml.jackson.databind.JsonMappingException;
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
import com.fasterxml.jackson.databind.exc.InvalidFormatException;
|
import com.fasterxml.jackson.databind.exc.InvalidFormatException;
|
||||||
import com.google.common.collect.Lists;
|
import com.google.common.collect.Lists;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
@ -272,6 +275,11 @@ public class GlobalExceptionHandler extends ResponseEntityExceptionHandler {
|
|||||||
throw e;
|
throw e;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ExceptionHandler(RecordException.class)
|
||||||
|
public String recordException(HttpServletRequest request, HttpServletResponse response, Throwable e) {
|
||||||
|
return e.getMessage();
|
||||||
|
}
|
||||||
|
|
||||||
@ExceptionHandler(Throwable.class)
|
@ExceptionHandler(Throwable.class)
|
||||||
public Result handleAllException(HttpServletRequest request, HttpServletResponse response, Throwable e) {
|
public Result handleAllException(HttpServletRequest request, HttpServletResponse response, Throwable e) {
|
||||||
if (shouldRecordException(request)) {
|
if (shouldRecordException(request)) {
|
||||||
@ -289,8 +297,8 @@ public class GlobalExceptionHandler extends ResponseEntityExceptionHandler {
|
|||||||
|
|
||||||
private Result buildResult(HttpServletRequest request, HttpServletResponse response, Throwable e) {
|
private Result buildResult(HttpServletRequest request, HttpServletResponse response, Throwable e) {
|
||||||
response.setHeader(CACHE_CONTROL, "no-store");
|
response.setHeader(CACHE_CONTROL, "no-store");
|
||||||
response.setStatus(251);
|
response.setStatus(512);
|
||||||
ApiResult<Object> err = err(251, "discovery server internal error");
|
ApiResult<Object> err = err(512, "discovery server internal error");
|
||||||
Map<String, ExceptionWrapper> recordExceptionMap = new HashMap<>();
|
Map<String, ExceptionWrapper> recordExceptionMap = new HashMap<>();
|
||||||
recordExceptionMap.put(StringUtils.hasText(applicationName) ? applicationName : "not applicationName found", rebuildThrowable(e, request));
|
recordExceptionMap.put(StringUtils.hasText(applicationName) ? applicationName : "not applicationName found", rebuildThrowable(e, request));
|
||||||
err.setData(recordExceptionMap);
|
err.setData(recordExceptionMap);
|
||||||
|
|||||||
@ -0,0 +1,22 @@
|
|||||||
|
package cn.axzo.framework.core;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 记录异常
|
||||||
|
*
|
||||||
|
* @author wangli
|
||||||
|
* @since 2024/6/25 21:06
|
||||||
|
*/
|
||||||
|
public class RecordException extends RuntimeException {
|
||||||
|
public RecordException(String message) {
|
||||||
|
super(message);
|
||||||
|
}
|
||||||
|
|
||||||
|
public RecordException(String message, Throwable cause) {
|
||||||
|
super(message, cause);
|
||||||
|
}
|
||||||
|
|
||||||
|
public RecordException(Throwable cause) {
|
||||||
|
super(cause);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user