Skip to content

Commit

Permalink
20240717
Browse files Browse the repository at this point in the history
  • Loading branch information
ldcsaa committed Jul 17, 2024
1 parent f52ce42 commit 61ff51f
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,30 +20,30 @@ public class ServiceException extends RuntimeException
public static final int NOT_EXIST = 404;
/** 参数校验失败 */
public static final int PARAM_VERIFY_ERROR = 409;
/** 参数验证错误 */
public static final int PARAM_VALIDATION_ERROR = 410;
/** 请求参数非法 */
public static final int PARAMS_ERROR = 411;
/** 重复请求 */
public static final int REPEATED_REQ_ERROR = 412;
/** 服务器内部错误 */
public static final int GENERAL_ERROR = 500;
/** 参数验证错误 */
public static final int PARAM_VALIDATION_ERROR = 501;
/** 接口未实现 */
public static final int NOT_IMPLEMENTED = 502;
public static final int NOT_IMPLEMENTED = 513;
/** 频次超限 */
public static final int FREQUENCY_LIMIT_ERROR = 503;
public static final int FREQUENCY_LIMIT_ERROR = 514;
/** 拒绝访问 */
public static final int FORBID_ERROR = 504;
/** 请求参数非法 */
public static final int PARAMS_ERROR = 505;
/** 重复请求 */
public static final int REPEATED_REQ_ERROR = 506;
public static final int FORBID_ERROR = 515;
/** 访问被限流 */
public static final int TRAFFIC_LIMIT_ERROR = 507;
public static final int TRAFFIC_LIMIT_ERROR = 516;
/** 接口不支持 */
public static final int NOT_SUPPORTED = 508;
public static final int NOT_SUPPORTED = 517;
/** 禁止更新 */
public static final int FORBID_UPDATE_ERROR = 509;
public static final int FORBID_UPDATE_ERROR = 518;
/** 拒绝写入 */
public static final int READ_ONLY_ERROR = 510;
public static final int READ_ONLY_ERROR = 519;
/** 调用超时 */
public static final int TIMEOUT_ERROR = 511;
public static final int TIMEOUT_ERROR = 520;
/** 应用程序编号验证错误 */
public static final int APPCODE_CHECK_ERROR = 601;
/** 应用程序编号不存在 */
Expand All @@ -70,8 +70,10 @@ public class ServiceException extends RuntimeException
public static final ServiceException BAD_REQUEST_EXCEPTION = new ServiceException("非法请求", BAD_REQUEST);
public static final ServiceException NOT_EXIST_EXCEPTION = new ServiceException("目标不存在", NOT_EXIST);
public static final ServiceException PARAM_VERIFY_EXCEPTION = new ServiceException("参数校验失败", PARAM_VERIFY_ERROR);
public static final ServiceException GENERAL_EXCEPTION = new ServiceException("服务器内部错误", GENERAL_ERROR);
public static final ServiceException PARAM_VALIDATION_EXCEPTION = new ServiceException("参数验证错误", PARAM_VALIDATION_ERROR);
public static final ServiceException PARAMS_EXCEPTION = new ServiceException("请求参数非法", PARAMS_ERROR);
public static final ServiceException REPEATED_REQ_EXCEPTION = new ServiceException("重复请求", REPEATED_REQ_ERROR);
public static final ServiceException GENERAL_EXCEPTION = new ServiceException("服务器内部错误", GENERAL_ERROR);
public static final ServiceException NOT_IMPLEMENTED_EXCEPTION = new ServiceException("接口未实现", NOT_IMPLEMENTED);
public static final ServiceException NOT_SUPPORTED_EXCEPTION = new ServiceException("接口不支持", NOT_SUPPORTED);
public static final ServiceException FORBID_SERVICE_EXCEPTION = new ServiceException("禁止更新", FORBID_UPDATE_ERROR);
Expand All @@ -81,8 +83,6 @@ public class ServiceException extends RuntimeException
public static final ServiceException AUTHEN_EXCEPTION = new ServiceException("用户认证失败", AUTHEN_ERROR);
public static final ServiceException AUTHOR_EXCEPTION = new ServiceException("授权验证失败", AUTHOR_ERROR);
public static final ServiceException NETWORK_EXCEPTION = new ServiceException("网络错误", NETWORK_ERROR);
public static final ServiceException PARAMS_EXCEPTION = new ServiceException("请求参数非法", PARAMS_ERROR);
public static final ServiceException REPEATED_REQ_EXCEPTION = new ServiceException("请勿重复请求", REPEATED_REQ_ERROR);
public static final ServiceException TIMEOUT_EXCEPTION = new ServiceException("调用超时", TIMEOUT_ERROR);
public static final ServiceException OUTER_API_CALL_EXCEPTION = new ServiceException("外部服务调用失败", OUTER_API_CALL_FAIL);
public static final ServiceException INNER_API_CALL_EXCEPTION = new ServiceException("内部服务调用失败", INNER_API_CALL_FAIL);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@
import java.io.IOException;
import java.io.PrintWriter;

import org.springframework.http.HttpStatus;

import com.alibaba.csp.sentinel.adapter.spring.webmvc.callback.BlockExceptionHandler;
import com.alibaba.csp.sentinel.slots.block.AbstractRule;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.fastjson2.JSON;

import io.github.hpsocket.soa.framework.core.exception.ServiceException;
import io.github.hpsocket.soa.framework.web.holder.AppConfigHolder;
import io.github.hpsocket.soa.framework.web.model.Response;
import io.github.hpsocket.soa.framework.web.support.WebServerHelper;
import jakarta.servlet.http.HttpServletRequest;
Expand All @@ -26,11 +29,12 @@ public class DefaultSentinelBlockExceptionHandler implements BlockExceptionHandl
public void handle(HttpServletRequest request, HttpServletResponse response, BlockException e) throws IOException
{
AbstractRule rule = e.getRule();
ServiceException se = wrapUnimportantException("接口繁忙" + ((rule != null) ? (": " + rule.getResource()) : ""), FREQUENCY_LIMIT_ERROR, e);
String msg = String.format("接口繁忙 - %s:%s", AppConfigHolder.getAppName(), (rule != null) ? rule.getResource() : "");
ServiceException se = wrapUnimportantException(msg, FREQUENCY_LIMIT_ERROR, e);

logServiceException(log, se, false);

response.setStatus(FREQUENCY_LIMIT_ERROR);
response.setStatus(HttpStatus.TOO_MANY_REQUESTS.value());
response.setContentType("application/json; charset=utf-8");

try(PrintWriter out = response.getWriter())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public Response<?> handleMethodArgumentNotValidException(HttpServletRequest requ

/** {@linkplain Exception} 异常处理器 */
@Override
@ResponseStatus(value = HttpStatus.EXPECTATION_FAILED)
@ResponseStatus(value = HttpStatus.SERVICE_UNAVAILABLE)
@ExceptionHandler({Exception.class})
public Response<?> handleException(HttpServletRequest request, HttpServletResponse response, Exception e)
{
Expand All @@ -45,7 +45,7 @@ public Response<?> handleException(HttpServletRequest request, HttpServletRespon
else
{
log.error(e.getMessage(), e);
return TracingHelper.createExceptionResponse(e, HttpStatus.EXPECTATION_FAILED);
return TracingHelper.createExceptionResponse(e, HttpStatus.SERVICE_UNAVAILABLE);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class CloudControllerSpecificExceptionAdvice implements Ordered
public static final int ORDER = ControllerGlobalExceptionAdvice.ORDER - 200;

/** {@linkplain FeignException} 异常处理器 */
@ResponseStatus(value = HttpStatus.EXPECTATION_FAILED)
@ResponseStatus(value = HttpStatus.SERVICE_UNAVAILABLE)
@ExceptionHandler({FeignException.class})
public Response<?> handleException(HttpServletRequest request, HttpServletResponse response, FeignException e)
{
Expand All @@ -37,7 +37,7 @@ public Response<?> handleException(HttpServletRequest request, HttpServletRespon
ServiceException se = null;

if(e instanceof RetryableException || e instanceof FeignServerException.GatewayTimeout)
se = new ServiceException("服务调用超时", INNER_API_CALL_FAIL, e);
se = new ServiceException("内部服务不可用", INNER_API_CALL_FAIL, e);
else
se = wrapServiceException(INNER_API_CALL_EXCEPTION, e);

Expand All @@ -48,7 +48,7 @@ public Response<?> handleException(HttpServletRequest request, HttpServletRespon
else
{
log.error(e.getMessage(), e);
return TracingHelper.createExceptionResponse(e, HttpStatus.EXPECTATION_FAILED);
return TracingHelper.createExceptionResponse(e, HttpStatus.SERVICE_UNAVAILABLE);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public Response<?> handleBlockException(HttpServletRequest request, HttpServletR

/** {@linkplain UndeclaredThrowableException} 异常处理器 */
@Override
@ResponseStatus(value = HttpStatus.EXPECTATION_FAILED)
@ResponseStatus(value = HttpStatus.SERVICE_UNAVAILABLE)
@ExceptionHandler({UndeclaredThrowableException.class})
public Response<?> handleUndeclaredThrowableException(HttpServletRequest request, HttpServletResponse response, UndeclaredThrowableException e)
{
Expand All @@ -48,13 +48,13 @@ public Response<?> handleUndeclaredThrowableException(HttpServletRequest request
else
{
log.error(e.getMessage(), e);
return TracingHelper.createExceptionResponse(e, HttpStatus.EXPECTATION_FAILED);
return TracingHelper.createExceptionResponse(e, HttpStatus.SERVICE_UNAVAILABLE);
}
}

/** {@linkplain RuntimeException} 异常处理器 */
@Override
@ResponseStatus(value = HttpStatus.EXPECTATION_FAILED)
@ResponseStatus(value = HttpStatus.SERVICE_UNAVAILABLE)
@ExceptionHandler({RuntimeException.class})
public Response<?> handleRuntimeException(HttpServletRequest request, HttpServletResponse response, RuntimeException e)
{
Expand All @@ -63,7 +63,7 @@ public Response<?> handleRuntimeException(HttpServletRequest request, HttpServle
else
{
log.error(e.getMessage(), e);
return TracingHelper.createExceptionResponse(e, HttpStatus.EXPECTATION_FAILED);
return TracingHelper.createExceptionResponse(e, HttpStatus.SERVICE_UNAVAILABLE);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,28 +57,14 @@ public Exception decode(String methodKey, feign.Response response)

Class<?> clazz = null;

try
{
clazz = Class.forName(info.getException());
}
catch(Exception e)
{

}
try {clazz = Class.forName(info.getException());} catch(Exception e) {}

if(clazz == null)
return new ServiceException(String.format("%s: %s -> %s", desc, info.getException(), info.getMessage()), INNER_API_CALL_EXCEPTION);

Constructor<?> cstor = null;

try
{
cstor = clazz.getDeclaredConstructor(String.class);
}
catch(Exception e)
{

}
try {cstor = clazz.getDeclaredConstructor(String.class);} catch(Exception e) {}

if(cstor == null)
return new ServiceException(String.format("%s: %s -> %s", desc, info.getException(), info.getMessage()), INNER_API_CALL_EXCEPTION);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public static final void fillMdcAttr(MdcAttr mdcAttr, String tracingInfo)
if(GeneralHelper.isStrNotEmpty(key))
{
String value = CryptHelper.urlDecode(field.substring(i + 1).trim());
mdcAttr.set(key, value);;
mdcAttr.set(key, value);
}
}
}
Expand Down Expand Up @@ -93,7 +93,7 @@ public static final void setRequestAttribute(MdcAttr mdcAttr, HttpServletRequest

public static final Response<CloudExceptionInfo> createExceptionResponse(Exception e)
{
return createExceptionResponse(e, HttpStatus.EXPECTATION_FAILED);
return createExceptionResponse(e, HttpStatus.SERVICE_UNAVAILABLE);
}

public static final Response<CloudExceptionInfo> createExceptionResponse(Exception e, HttpStatus httpStatus)
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<url>https://github.com/ldcsaa/hp-soa</url>

<properties>
<revision>1.0.9</revision>
<revision>1.0.10</revision>
<hp-soa.version>${revision}</hp-soa.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
Expand Down

0 comments on commit 61ff51f

Please sign in to comment.