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

NPE in OpenAIClient: HttpTransportException.getStatusCode() null auto-unboxing

Open richuV opened this issue 8 months ago • 0 comments

Bug Description

OpenAIClient.java line 442-443 passes HttpTransportException.getStatusCode() (return type Integer, nullable) directly to OpenAIException.create(int, ...) (parameter type int). When the exception is constructed without a status code (e.g. connection failure, DNS error, timeout), getStatusCode() returns null, and Java auto-unboxing throws a NullPointerException.

Stack Trace

Cannot invoke "java.lang.Integer.intValue()" because the return value of
"io.agentscope.core.model.transport.HttpTransportException.getStatusCode()" is null

Root Cause

HttpTransportException has constructors that leave statusCode = null:

public HttpTransportException(String message) {
    super(message);
    this.statusCode = null;  // no HTTP status code
}

public HttpTransportException(String message, Throwable cause) {
    super(message, cause);
    this.statusCode = null;  // no HTTP status code
}

But the streaming error handler in OpenAIClient assumes it's always non-null:

.onErrorMap(ex -> {
    if (ex instanceof HttpTransportException) {
        return OpenAIException.create(
            ((HttpTransportException) ex).getStatusCode(),  // NPE here when null
            "HTTP transport error during streaming: " + ex.getMessage(),
            null,
            ((HttpTransportException) ex).getResponseBody());
    }
    return ex;
});

Suggested Fix

.onErrorMap(ex -> {
    if (ex instanceof HttpTransportException hte) {
        Integer code = hte.getStatusCode();
        String msg = "HTTP transport error during streaming: " + ex.getMessage();
        if (code != null) {
            return OpenAIException.create(code, msg, null, hte.getResponseBody());
        }
        return new OpenAIException(msg, ex);
    }
    return ex;
});

Environment

  • agentscope-core version: 1.0.10-SNAPSHOT
  • Java: 21

richuV avatar Mar 09 '26 02:03 richuV