Skip to content

Commit 1b98403

Browse files
committed
feat(ws): support response code
1 parent 26ce408 commit 1b98403

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

src/main/java/com/alibaba/dashscope/common/DashScopeResult.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,34 @@ protected <T extends Result> T fromResponse(Protocol protocol, NetworkResponse r
3636
if (headers.has(ApiKeywords.TASKID)) {
3737
this.setRequestId(headers.get(ApiKeywords.TASKID).getAsString());
3838
}
39+
// Extract status_code, code and message from header
40+
if (headers.has(ApiKeywords.STATUS_CODE)) {
41+
this.setStatusCode(
42+
headers.get(ApiKeywords.STATUS_CODE).isJsonNull()
43+
? null
44+
: headers.get(ApiKeywords.STATUS_CODE).getAsInt());
45+
} else {
46+
// Set default status code
47+
this.setStatusCode(200);
48+
}
49+
if (headers.has(ApiKeywords.ERROR_CODE)) {
50+
this.setCode(
51+
headers.get(ApiKeywords.ERROR_CODE).isJsonNull()
52+
? ""
53+
: headers.get(ApiKeywords.ERROR_CODE).getAsString());
54+
} else {
55+
// Set default empty string for successful responses
56+
this.setCode("");
57+
}
58+
if (headers.has(ApiKeywords.ERROR_MESSAGE)) {
59+
this.setMessage(
60+
headers.get(ApiKeywords.ERROR_MESSAGE).isJsonNull()
61+
? ""
62+
: headers.get(ApiKeywords.ERROR_MESSAGE).getAsString());
63+
} else {
64+
// Set default empty string for successful responses
65+
this.setMessage("");
66+
}
3967
}
4068
if (jsonObject.has(ApiKeywords.PAYLOAD)) {
4169
JsonObject payload = jsonObject.getAsJsonObject(ApiKeywords.PAYLOAD);

src/main/java/com/alibaba/dashscope/utils/ApiKeywords.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,8 @@ public class ApiKeywords {
9999

100100
public static final String ERROR_NAME = "error_name";
101101

102+
public static final String ERROR_CODE = "error_code";
103+
102104
public static final String ERROR_MESSAGE = "error_message";
103105

104106
public static final String OUTPUT = "output";

0 commit comments

Comments
 (0)