36 lines
1.3 KiB
Java
36 lines
1.3 KiB
Java
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 错误处理器
|
||
*
|
||
* <p>设计要点:
|
||
* <ol>
|
||
* <li>显式重写 hasError() 委托父类判断(4xx/5xx → true),让 handleError() 在错误时被调用</li>
|
||
* <li>handleError() 不抛 RestClientException,由业务层(WechatMiniProgramServiceImpl)
|
||
* 根据 status code 判断如何处理</li>
|
||
* <li>不在此处读 body(避免流消费,由 LoggingInterceptor 统一处理日志)</li>
|
||
* </ol>
|
||
*/
|
||
@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());
|
||
}
|
||
}
|