Skip to content

Commit 23129d9

Browse files
authored
feat(smithy-client): add isInstance method for ServiceException class (#1484)
* feat(smithy-client): add isInstance helper for ServiceException class * chore(smithy-client): add changeset * feat(smithy-client): add support for instanceof operator * chore(smithy-client): explicit access modifier
1 parent e42eb49 commit 23129d9

File tree

3 files changed

+73
-0
lines changed

3 files changed

+73
-0
lines changed

.changeset/warm-pans-cross.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@smithy/smithy-client": minor
3+
---
4+
5+
feat: type check helper method to to know if something is an instance of the ServiceException class

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

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,57 @@ it("ExceptionOptionType allows specifying message", () => {
3434
expect(exception.code).toBe("code");
3535
});
3636

37+
describe("ServiceException type checking", () => {
38+
const error = new ServiceException({
39+
name: "Error",
40+
$fault: "client",
41+
$metadata: {},
42+
});
43+
44+
const duckTyped = {
45+
$fault: "server",
46+
$metadata: {},
47+
};
48+
49+
describe("isInstance", () => {
50+
it("should return true for ServiceException instances", () => {
51+
expect(ServiceException.isInstance(error)).toBe(true);
52+
});
53+
54+
it("should return true for duck-typed objects", () => {
55+
expect(ServiceException.isInstance(duckTyped)).toBe(true);
56+
});
57+
58+
it("should return false for null or undefined", () => {
59+
expect(ServiceException.isInstance(null)).toBe(false);
60+
expect(ServiceException.isInstance(undefined)).toBe(false);
61+
});
62+
63+
it("should return false for invalid $fault values", () => {
64+
expect(ServiceException.isInstance({ $fault: "invalid", $metadata: {} })).toBe(false);
65+
});
66+
67+
it("should return false for missing properties", () => {
68+
expect(ServiceException.isInstance({ $fault: "client" })).toBe(false);
69+
expect(ServiceException.isInstance({ $metadata: {} })).toBe(false);
70+
});
71+
});
72+
73+
describe("instanceof", () => {
74+
it("should return true for ServiceException instances", () => {
75+
expect(error instanceof ServiceException).toBe(true);
76+
});
77+
78+
it("should return true for duck-typed objects", () => {
79+
expect(duckTyped instanceof ServiceException).toBe(true);
80+
});
81+
82+
it("should return false for invalid objects", () => {
83+
expect({} instanceof ServiceException).toBe(false);
84+
});
85+
});
86+
});
87+
3788
describe("decorateServiceException", () => {
3889
const exception = new ServiceException({
3990
name: "Error",

packages/smithy-client/src/exceptions.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,23 @@ export class ServiceException extends Error implements SmithyException, Metadata
3737
this.$fault = options.$fault;
3838
this.$metadata = options.$metadata;
3939
}
40+
41+
/**
42+
* Checks if a value is an instance of ServiceException (duck typed)
43+
*/
44+
public static isInstance(value: unknown): value is ServiceException {
45+
if (!value) return false;
46+
const candidate = value as ServiceException;
47+
return (
48+
Boolean(candidate.$fault) &&
49+
Boolean(candidate.$metadata) &&
50+
(candidate.$fault === "client" || candidate.$fault === "server")
51+
);
52+
}
53+
54+
public static [Symbol.hasInstance](instance: unknown) {
55+
return ServiceException.isInstance(instance);
56+
}
4057
}
4158

4259
/**

0 commit comments

Comments
 (0)