Skip to content

Commit 7d97314

Browse files
fix(core): handle empty error response body in AwsQuery protocol deserialization (#7766)
1 parent 95d1cc6 commit 7d97314

2 files changed

Lines changed: 47 additions & 3 deletions

File tree

‎packages-internal/core/src/submodules/protocols/query/AwsQueryProtocol.spec.ts‎

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { TypeRegistry } from "@smithy/core/schema";
22
import { HttpResponse } from "@smithy/protocol-http";
3-
import { ServiceException, ServiceExceptionOptions } from "@smithy/smithy-client";
4-
import { StaticErrorSchema } from "@smithy/types";
3+
import type { ServiceExceptionOptions } from "@smithy/smithy-client";
4+
import { ServiceException } from "@smithy/smithy-client";
5+
import type { StaticErrorSchema } from "@smithy/types";
56
import { describe, expect, test as it } from "vitest";
67

78
import { context } from "../test-schema.spec";
@@ -126,3 +127,46 @@ describe(AwsQueryProtocol.name, () => {
126127
expect(actual.Error).toEqual(expected.Error);
127128
});
128129
});
130+
131+
it("should not crash when error response body is empty", async () => {
132+
const httpResponse = new HttpResponse({
133+
statusCode: 500,
134+
headers: {
135+
"content-type": "text/xml",
136+
},
137+
body: Buffer.from(""),
138+
});
139+
140+
const protocol = new AwsQueryProtocol({
141+
version: "1999-12-31",
142+
defaultNamespace: "com.amazonaws.giraffes",
143+
xmlNamespace: "ns",
144+
});
145+
146+
const actual = await protocol
147+
.deserializeResponse(
148+
{
149+
namespace: "ns",
150+
name: "Empty",
151+
traits: 0,
152+
input: "unit" as const,
153+
output: [3, "ns", "EmptyOutput", 0, [], []],
154+
},
155+
context,
156+
httpResponse
157+
)
158+
.catch((e) => {
159+
return e;
160+
});
161+
162+
// The error should be a proper ServiceException, not a raw TypeError
163+
expect(actual).toBeDefined();
164+
expect(actual).not.toBeInstanceOf(TypeError);
165+
expect(actual.$metadata.httpStatusCode).toBe(500);
166+
expect(actual.message).toBe("Unknown");
167+
expect(actual.Error).toEqual({
168+
Type: undefined,
169+
Code: undefined,
170+
Message: "Unknown",
171+
});
172+
});

‎packages-internal/core/src/submodules/protocols/query/AwsQueryProtocol.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ export class AwsQueryProtocol extends RpcProtocol {
146146
metadata: ResponseMetadata
147147
): Promise<never> {
148148
const errorIdentifier = this.loadQueryErrorCode(response, dataObject) ?? "Unknown";
149-
const errorData = this.loadQueryError(dataObject);
149+
const errorData = this.loadQueryError(dataObject) ?? {};
150150
const message = this.loadQueryErrorMessage(dataObject);
151151
errorData.message = message;
152152
errorData.Error = {

0 commit comments

Comments
 (0)