diff --git a/backend-single/src/main/java/com/emotion/config/WechatResponseErrorHandler.java b/backend-single/src/main/java/com/emotion/config/WechatResponseErrorHandler.java new file mode 100644 index 0000000..ed88a6a --- /dev/null +++ b/backend-single/src/main/java/com/emotion/config/WechatResponseErrorHandler.java @@ -0,0 +1,35 @@ +package com.emotion.config; + +import lombok.extern.slf4j.Slf4j; +import org.springframework.http.client.ClientHttpResponse; +import org.springframework.web.client.DefaultResponseErrorHandler; + +import java.io.IOException; + +/** + * 微信 API 错误处理器 + * + *

设计要点: + *

    + *
  1. 显式重写 hasError() 委托父类判断(4xx/5xx → true),让 handleError() 在错误时被调用
  2. + *
  3. handleError() 不抛 RestClientException,由业务层(WechatMiniProgramServiceImpl) + * 根据 status code 判断如何处理
  4. + *
  5. 不在此处读 body(避免流消费,由 LoggingInterceptor 统一处理日志)
  6. + *
+ */ +@Slf4j +public class WechatResponseErrorHandler extends DefaultResponseErrorHandler { + + @Override + public boolean hasError(ClientHttpResponse response) throws IOException { + // 委托父类 DefaultResponseErrorHandler.hasError():4xx/5xx 返回 true,其他返回 false + return super.hasError(response); + } + + @Override + public void handleError(ClientHttpResponse response) throws IOException { + // 不抛异常;body 已在 LoggingInterceptor 中读取并脱敏记录 + // 业务层会通过 response.getStatusCode() 判断 + log.warn("[WeChatAPI] HTTP error response, status={}", response.getStatusCode()); + } +}