From e87f2b3c416b5d8a61e4f451a74c8b473c2dfa5e Mon Sep 17 00:00:00 2001 From: sid <48153483+siddsriv@users.noreply.github.com> Date: Mon, 13 Jan 2025 15:19:18 -0500 Subject: [PATCH] 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 --- .changeset/rich-emus-cry.md | 5 +++++ packages/smithy-client/src/exceptions.spec.ts | 11 +++++++++++ packages/smithy-client/src/exceptions.ts | 7 ++++--- 3 files changed, 20 insertions(+), 3 deletions(-) create mode 100644 .changeset/rich-emus-cry.md diff --git a/.changeset/rich-emus-cry.md b/.changeset/rich-emus-cry.md new file mode 100644 index 00000000000..8d7e2cf98de --- /dev/null +++ b/.changeset/rich-emus-cry.md @@ -0,0 +1,5 @@ +--- +"@smithy/smithy-client": patch +--- + +prototype chain fallback for service exception diff --git a/packages/smithy-client/src/exceptions.spec.ts b/packages/smithy-client/src/exceptions.spec.ts index 6d3ee8989d3..e0f23aeaa8b 100644 --- a/packages/smithy-client/src/exceptions.spec.ts +++ b/packages/smithy-client/src/exceptions.spec.ts @@ -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", () => { diff --git a/packages/smithy-client/src/exceptions.ts b/packages/smithy-client/src/exceptions.ts index 35417a8b688..b0878bb52fa 100644 --- a/packages/smithy-client/src/exceptions.ts +++ b/packages/smithy-client/src/exceptions.ts @@ -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")) ); }