Skip to content

fix(smithy-client): add prototype chain fallback for service exception #1503

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 6 commits into from
Jan 13, 2025
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/rich-emus-cry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@smithy/smithy-client": patch
---

prototype chain fallback for service exception
11 changes: 11 additions & 0 deletions packages/smithy-client/src/exceptions.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,17 @@ describe("Exception Hierarchy Tests", () => {
expect(clientException instanceof ClientServiceException).toBe(true);
expect(clientException instanceof ModeledClientServiceException).toBe(false);
});

it("should handle exceptions where name indicates error type", () => {
const egError = new ClientServiceException();
egError.name = "EgTooLarge"; // specific error type name
// Should maintain instanceof chain
expect(egError instanceof Error).toBe(true);
expect(egError instanceof ServiceException).toBe(true);
expect(egError instanceof ClientServiceException).toBe(true);
// The name property can be used to identify specific error types
expect(egError.name).toBe("EgTooLarge");
});
});

describe("ModeledClientServiceException Instance Tests", () => {
Expand Down
7 changes: 4 additions & 3 deletions packages/smithy-client/src/exceptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,10 @@ export class ServiceException extends Error implements SmithyException, Metadata
if (!value) return false;
const candidate = value as ServiceException;
return (
Boolean(candidate.$fault) &&
Boolean(candidate.$metadata) &&
(candidate.$fault === "client" || candidate.$fault === "server")
ServiceException.prototype.isPrototypeOf(candidate) ||
(Boolean(candidate.$fault) &&
Boolean(candidate.$metadata) &&
(candidate.$fault === "client" || candidate.$fault === "server"))
);
}

Expand Down
Loading