-
Notifications
You must be signed in to change notification settings - Fork 1k
fix(dashscope): preserve SSE error response body #2278
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -270,33 +270,36 @@ public Flux<DashScopeResponse> stream( | |
| .build(); | ||
|
|
||
| return transport.stream(httpRequest) | ||
| .map( | ||
| data -> { | ||
| .<ParsedStreamResponse>handle( | ||
| (data, sink) -> { | ||
| try { | ||
| // Decrypt response if encryption is enabled | ||
| if (finalEncryptionContext != null) { | ||
| data = decryptResponse(data, finalEncryptionContext); | ||
| } | ||
| return JsonUtils.getJsonCodec() | ||
| .fromJson(data, DashScopeResponse.class); | ||
| sink.next( | ||
| new ParsedStreamResponse( | ||
| data, | ||
| JsonUtils.getJsonCodec() | ||
| .fromJson( | ||
| data, | ||
| DashScopeResponse.class))); | ||
| } catch (JsonException e) { | ||
| log.warn( | ||
| "Failed to parse SSE data: {}. Error: {}", | ||
| data, | ||
| e.getMessage()); | ||
| // Return null and filter out later | ||
| return null; | ||
| } | ||
| }) | ||
| .filter(response -> response != null) | ||
| .handle( | ||
| (response, sink) -> { | ||
| (streamResponse, sink) -> { | ||
| DashScopeResponse response = streamResponse.response(); | ||
| if (response.isError()) { | ||
| sink.error( | ||
| new DashScopeHttpException( | ||
| "DashScope API error: " + response.getMessage(), | ||
| response.getCode(), | ||
| null)); | ||
| streamResponse.responseBody())); | ||
| } else { | ||
| sink.next(response); | ||
| } | ||
|
|
@@ -834,6 +837,8 @@ public DashScopeHttpClient build() { | |
| } | ||
| } | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [praise] Clean use of a private record to thread the raw response body alongside the parsed response through the reactive pipeline. Minimal change surface, no side channels, and makes the streaming error path symmetric with the sync |
||
|
|
||
| private record ParsedStreamResponse(String responseBody, DashScopeResponse response) {} | ||
|
|
||
| /** | ||
| * Exception thrown when DashScope HTTP operations fail. | ||
| */ | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -647,10 +647,26 @@ void testStreamErrorHandling() { | |
| && dashScopeHttpException.getErrorCode().equals(errorCode) | ||
| && dashScopeHttpException | ||
| .getMessage() | ||
| .equals("DashScope API error: " + errorMessage)) | ||
| .equals("DashScope API error: " + errorMessage) | ||
| && dashScopeHttpException | ||
| .getResponseBody() | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [praise] Good regression test — asserting
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [praise] Good regression test — asserting |
||
| .contains("\"request_id\":\"request_id_123\"")) | ||
| .verify(); | ||
| } | ||
|
|
||
| @Test | ||
| void testStreamIgnoresMalformedSseData() { | ||
| mockServer.enqueue( | ||
| new MockResponse() | ||
| .setResponseCode(200) | ||
| .setBody("data: malformed-json\\n\\n") | ||
| .setHeader("Content-Type", "text/event-stream")); | ||
|
|
||
| DashScopeRequest request = createTestRequest("qwen-plus", "test"); | ||
|
|
||
| StepVerifier.create(client.stream(request, null, null, null)).verifyComplete(); | ||
| } | ||
|
|
||
| @Test | ||
| void testHeaderOverride() throws Exception { | ||
| mockServer.enqueue( | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[praise] Clean use of a private record to thread the raw response body alongside the parsed response through the reactive pipeline. Minimal change surface, no side channels, and makes the streaming error path symmetric with the sync
execute()path at line 215. Well done.