Skip to content

Commit 49640d6

Browse files
authored
fix: allow error deserializers to populate error response body (#1180)
1 parent a0eede1 commit 49640d6

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

.changeset/lazy-olives-film.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@smithy/middleware-serde": patch
3+
---
4+
5+
allow deserializers to populate error response body

packages/middleware-serde/src/deserializerMiddleware.spec.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,12 @@ describe("deserializerMiddleware", () => {
7878
});
7979

8080
it("adds a hint about $response to the message of the thrown error", async () => {
81-
const exception = Object.assign(new Error("MockException"), mockNextResponse.response);
81+
const exception = Object.assign(new Error("MockException"), mockNextResponse.response, {
82+
$response: {
83+
body: "",
84+
},
85+
$responseBodyText: "oh no",
86+
});
8287
mockDeserializer.mockReset();
8388
mockDeserializer.mockRejectedValueOnce(exception);
8489
try {
@@ -88,6 +93,7 @@ describe("deserializerMiddleware", () => {
8893
expect(e.message).toContain(
8994
"to see the raw response, inspect the hidden field {error}.$response on this object."
9095
);
96+
expect(e.$response.body).toEqual("oh no");
9197
}
9298
});
9399
});

packages/middleware-serde/src/deserializerMiddleware.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,14 @@ export const deserializerMiddleware = <Input extends object, Output extends obje
3333
// only apply this to non-ServiceException.
3434
const hint = `Deserialization error: to see the raw response, inspect the hidden field {error}.$response on this object.`;
3535
error.message += "\n " + hint;
36+
37+
if (typeof error.$responseBodyText !== "undefined") {
38+
// if $responseBodyText was collected by the error parser, assign it to
39+
// replace the response body, because it was consumed and is now empty.
40+
if (error.$response) {
41+
error.$response.body = error.$responseBodyText;
42+
}
43+
}
3644
}
3745

3846
throw error;

0 commit comments

Comments
 (0)