Skip to content

Commit

Permalink
fix: allow error deserializers to populate error response body (#1180)
Browse files Browse the repository at this point in the history
  • Loading branch information
kuhe authored Mar 7, 2024
1 parent a0eede1 commit 49640d6
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/lazy-olives-film.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@smithy/middleware-serde": patch
---

allow deserializers to populate error response body
8 changes: 7 additions & 1 deletion packages/middleware-serde/src/deserializerMiddleware.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,12 @@ describe("deserializerMiddleware", () => {
});

it("adds a hint about $response to the message of the thrown error", async () => {
const exception = Object.assign(new Error("MockException"), mockNextResponse.response);
const exception = Object.assign(new Error("MockException"), mockNextResponse.response, {
$response: {
body: "",
},
$responseBodyText: "oh no",
});
mockDeserializer.mockReset();
mockDeserializer.mockRejectedValueOnce(exception);
try {
Expand All @@ -88,6 +93,7 @@ describe("deserializerMiddleware", () => {
expect(e.message).toContain(
"to see the raw response, inspect the hidden field {error}.$response on this object."
);
expect(e.$response.body).toEqual("oh no");
}
});
});
8 changes: 8 additions & 0 deletions packages/middleware-serde/src/deserializerMiddleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ export const deserializerMiddleware = <Input extends object, Output extends obje
// only apply this to non-ServiceException.
const hint = `Deserialization error: to see the raw response, inspect the hidden field {error}.$response on this object.`;
error.message += "\n " + hint;

if (typeof error.$responseBodyText !== "undefined") {
// if $responseBodyText was collected by the error parser, assign it to
// replace the response body, because it was consumed and is now empty.
if (error.$response) {
error.$response.body = error.$responseBodyText;
}
}
}

throw error;
Expand Down

0 comments on commit 49640d6

Please sign in to comment.