Skip to content

Commit

Permalink
fix(smithy-client): add prototype chain fallback for service exception (
Browse files Browse the repository at this point in the history
#1503)

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

* chore: changeset

* chore: move to isInstance

* test(smithy-client): add test case for different class name and name prop

* chore: formatting

* test(smithy-client): update test for ClientServiceException
  • Loading branch information
siddsriv authored Jan 13, 2025
1 parent 359a30b commit e87f2b3
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
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

0 comments on commit e87f2b3

Please sign in to comment.