Skip to content

Commit e87f2b3

Browse files
authored
fix(smithy-client): add prototype chain fallback for service exception (#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
1 parent 359a30b commit e87f2b3

File tree

3 files changed

+20
-3
lines changed

3 files changed

+20
-3
lines changed

.changeset/rich-emus-cry.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@smithy/smithy-client": patch
3+
---
4+
5+
prototype chain fallback for service exception

packages/smithy-client/src/exceptions.spec.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,17 @@ describe("Exception Hierarchy Tests", () => {
9797
expect(clientException instanceof ClientServiceException).toBe(true);
9898
expect(clientException instanceof ModeledClientServiceException).toBe(false);
9999
});
100+
101+
it("should handle exceptions where name indicates error type", () => {
102+
const egError = new ClientServiceException();
103+
egError.name = "EgTooLarge"; // specific error type name
104+
// Should maintain instanceof chain
105+
expect(egError instanceof Error).toBe(true);
106+
expect(egError instanceof ServiceException).toBe(true);
107+
expect(egError instanceof ClientServiceException).toBe(true);
108+
// The name property can be used to identify specific error types
109+
expect(egError.name).toBe("EgTooLarge");
110+
});
100111
});
101112

102113
describe("ModeledClientServiceException Instance Tests", () => {

packages/smithy-client/src/exceptions.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,10 @@ export class ServiceException extends Error implements SmithyException, Metadata
4545
if (!value) return false;
4646
const candidate = value as ServiceException;
4747
return (
48-
Boolean(candidate.$fault) &&
49-
Boolean(candidate.$metadata) &&
50-
(candidate.$fault === "client" || candidate.$fault === "server")
48+
ServiceException.prototype.isPrototypeOf(candidate) ||
49+
(Boolean(candidate.$fault) &&
50+
Boolean(candidate.$metadata) &&
51+
(candidate.$fault === "client" || candidate.$fault === "server"))
5152
);
5253
}
5354

0 commit comments

Comments
 (0)