Skip to content

Commit 2a5a188

Browse files
authored
revert(protocol/websocket): keep fixed status code 44 for handshake failure (#257)
* revert(protocol/websocket): keep fixed status code 44 for handshake failure The 4xx fail-fast change started surfacing the upstream HTTP status code (e.g. 401) in the ApiException status, which breaks callers that rely on the fixed websocket failure status code 44. Restore Constants.DASHSCOPE_WEBSOCKET_FAILED_STATUS_CODE as the reported status code. The handshake HTTP status code is still extracted, but it is now only used to decide whether a retry is pointless. * fix: test case for websocket error code
1 parent 2440e14 commit 2a5a188

2 files changed

Lines changed: 10 additions & 8 deletions

File tree

src/main/java/com/alibaba/dashscope/protocol/okhttp/OkHttpWebSocketClient.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ private void establishWebSocketClient(
206206
httpStatusCode = extractHttpStatusCode(ex);
207207
// The server answered the handshake with a client error (401, 403, 429, ...): retrying
208208
// cannot change the outcome and would only add pressure on a server already refusing us,
209-
// so surface the status code to the caller right away.
209+
// so give up immediately.
210210
if (isClientError(httpStatusCode, errorMessage)) {
211211
log.warn(
212212
"Websocket handshake refused with http status {}, will not retry: {}",
@@ -228,14 +228,13 @@ private void establishWebSocketClient(
228228
}
229229
}
230230
}
231+
// The handshake status code only drives the retry decision above: callers rely on the fixed
232+
// websocket failure status code, so it must not leak into the reported status.
231233
throw new ApiException(
232234
Status.builder()
233235
.code("ConnectionError")
234236
.message(errorMessage)
235-
.statusCode(
236-
httpStatusCode > 0
237-
? httpStatusCode
238-
: Constants.DASHSCOPE_WEBSOCKET_FAILED_STATUS_CODE)
237+
.statusCode(Constants.DASHSCOPE_WEBSOCKET_FAILED_STATUS_CODE)
239238
.build());
240239
}
241240

src/test/java/com/alibaba/dashscope/TestFullDuplexErrorHandling.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -330,8 +330,10 @@ public void testHandshakeClientErrorIsNotRetried() throws ApiException, NoApiKey
330330
api.duplexCall(param).blockingForEach(msg -> {});
331331
});
332332
long elapsed = System.currentTimeMillis() - start;
333-
// The status code answered by the server is reported to the caller as is.
334-
assertEquals(429, thrown.getStatus().getStatusCode());
333+
// The handshake http status only drives the retry decision: callers keep seeing the fixed
334+
// websocket failure status code.
335+
assertEquals(
336+
Constants.DASHSCOPE_WEBSOCKET_FAILED_STATUS_CODE, thrown.getStatus().getStatusCode());
335337
// A client error is final: a single handshake must have been attempted.
336338
assertEquals(1, server.getRequestCount());
337339
// And no backoff must have been waited for.
@@ -359,7 +361,8 @@ public void testHandshakeServerErrorIsRetried() throws ApiException, NoApiKeyExc
359361
api.duplexCall(param).blockingForEach(msg -> {});
360362
});
361363
long elapsed = System.currentTimeMillis() - start;
362-
assertEquals(500, thrown.getStatus().getStatusCode());
364+
assertEquals(
365+
Constants.DASHSCOPE_WEBSOCKET_FAILED_STATUS_CODE, thrown.getStatus().getStatusCode());
363366
// A transient failure is retried up to the attempt limit.
364367
assertEquals(3, server.getRequestCount());
365368
// Two backoffs only: the last attempt must not be followed by a wait.

0 commit comments

Comments
 (0)