Skip to content

fix: allow error deserializers to populate error response body #1180

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

Merged
merged 1 commit into from
Mar 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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