|
@@ -6,6 +6,8 @@ import cn.dev33.satoken.exception.NotRoleException;
|
|
|
import com.kym.common.R;
|
|
import com.kym.common.R;
|
|
|
import com.kym.common.exception.BaseException;
|
|
import com.kym.common.exception.BaseException;
|
|
|
import com.kym.common.exception.BusinessException;
|
|
import com.kym.common.exception.BusinessException;
|
|
|
|
|
+import com.kym.common.exception.ChargeException;
|
|
|
|
|
+import com.kym.common.exception.PlatformCommunicationException;
|
|
|
import com.kym.common.exception.PlatformPushException;
|
|
import com.kym.common.exception.PlatformPushException;
|
|
|
import jakarta.validation.ConstraintViolation;
|
|
import jakarta.validation.ConstraintViolation;
|
|
|
import jakarta.validation.ConstraintViolationException;
|
|
import jakarta.validation.ConstraintViolationException;
|
|
@@ -142,10 +144,69 @@ public class GlobalExceptionHandler {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
- @ExceptionHandler(value = {Exception.class})
|
|
|
|
|
- public R handlerRestException(Exception e) {
|
|
|
|
|
- LOGGER.info(e.getMessage(), e);
|
|
|
|
|
- return R.failed();
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 充电业务异常处理
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param e 异常
|
|
|
|
|
+ * @return 异常结果
|
|
|
|
|
+ */
|
|
|
|
|
+ @ExceptionHandler(value = ChargeException.class)
|
|
|
|
|
+ @ResponseBody
|
|
|
|
|
+ public R<?> handleChargeException(ChargeException e) {
|
|
|
|
|
+ LOGGER.error("充电业务异常:{}", e.getMessage(), e);
|
|
|
|
|
+ return R.failed(e.getCode(), e.getMessage());
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 平台通信异常处理
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param e 异常
|
|
|
|
|
+ * @return 异常结果
|
|
|
|
|
+ */
|
|
|
|
|
+ @ExceptionHandler(value = PlatformCommunicationException.class)
|
|
|
|
|
+ @ResponseBody
|
|
|
|
|
+ public R<?> handlePlatformCommunicationException(PlatformCommunicationException e) {
|
|
|
|
|
+ LOGGER.error("平台通信异常:platform={}, api={}", e.getPlatformName(), e.getApiName(), e);
|
|
|
|
|
+ // 根据异常类型返回不同的错误信息
|
|
|
|
|
+ if (e instanceof PlatformCommunicationException.PlatformAuthException) {
|
|
|
|
|
+ return R.failed(503, "平台认证失败,请联系管理员");
|
|
|
|
|
+ } else if (e instanceof PlatformCommunicationException.PlatformTimeoutException) {
|
|
|
|
|
+ return R.failed(504, "平台响应超时,请稍后重试");
|
|
|
|
|
+ } else if (e instanceof PlatformCommunicationException.PlatformDataFormatException) {
|
|
|
|
|
+ return R.failed(500, "数据格式错误,请检查配置");
|
|
|
|
|
+ }
|
|
|
|
|
+ return R.failed(503, "外部服务不可用,请稍后重试");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 未知异常处理(兜底)
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param e 异常
|
|
|
|
|
+ * @return 异常结果
|
|
|
|
|
+ */
|
|
|
|
|
+ @ExceptionHandler(value = Exception.class)
|
|
|
|
|
+ @ResponseBody
|
|
|
|
|
+ public R<?> handlerRestException(Exception e) {
|
|
|
|
|
+ // 记录完整的堆栈信息用于排查问题
|
|
|
|
|
+ LOGGER.error("系统未知异常:URI={}, Message={}",
|
|
|
|
|
+ getCurrentRequestURI(), e.getMessage(), e);
|
|
|
|
|
+ return R.failed(HttpStatus.INTERNAL_SERVER_ERROR.value(), "系统繁忙,请稍后重试");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 获取当前请求 URI(用于日志记录)
|
|
|
|
|
+ */
|
|
|
|
|
+ private String getCurrentRequestURI() {
|
|
|
|
|
+ try {
|
|
|
|
|
+ var requestAttributes = org.springframework.web.context.request.RequestContextHolder.currentRequestAttributes();
|
|
|
|
|
+ if (requestAttributes instanceof org.springframework.web.context.request.ServletRequestAttributes) {
|
|
|
|
|
+ var request = ((org.springframework.web.context.request.ServletRequestAttributes) requestAttributes).getRequest();
|
|
|
|
|
+ return request.getRequestURI();
|
|
|
|
|
+ }
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ // 忽略异常,返回 unknown
|
|
|
|
|
+ }
|
|
|
|
|
+ return "unknown";
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|