agentscope-java icon indicating copy to clipboard operation
agentscope-java copied to clipboard

OpenAIClient 流式错误处理中 HttpTransportException.getStatusCode() 为 null 时触发 NPE

Open richuV opened this issue 9 months ago • 1 comments

问题描述

OpenAIClient.streamChatCompletion() 的 .onErrorMap 中,直接将 HttpTransportException.getStatusCode() 的返回值传给 OpenAIException.create(int, ...)。

但 HttpTransportException.getStatusCode() 返回类型是 Integer(可空),且部分构造函数(如 HttpTransportException(String message))会将 statusCode 设为 null。当 statusCode 为 null 时,自动拆箱触发 NullPointerException,吞掉了原始的流式通信错误,导致难以排查问题。

复现路径

OpenAIClient.java 约第 439-445 行:

.onErrorMap(ex -> {
    if (ex instanceof HttpTransportException) {
        return OpenAIException.create(
            ((HttpTransportException) ex).getStatusCode(), // Integer -> int 拆箱,null 时 NPE
            "HTTP transport error during streaming: " + ex.getMessage(),
            null, null);
    }
    return ex;
})

HttpTransportException.java:

private final Integer statusCode; // 可空
public Integer getStatusCode() { return statusCode; } // 返回 Integer

建议修复

为 OpenAIException 增加接受 Integer 参数的 create 重载,内部处理 null:

public static OpenAIException create(
        Integer statusCode, String message, String errorCode, String responseBody) {
    if (statusCode == null) {
        return new OpenAIException(message, null, errorCode, responseBody);
    }
    return create(statusCode.intValue(), message, errorCode, responseBody);
}

同时需要对应增加一个接受 Integer statusCode 的构造函数。

richuV avatar Feb 11 '26 17:02 richuV

please assign to me.

aihai avatar Feb 13 '26 10:02 aihai