Skip to content

Commit 1b7785b

Browse files
committed
refactor error handling in assertion functions
1 parent 016525c commit 1b7785b

File tree

4 files changed

+23
-6
lines changed

4 files changed

+23
-6
lines changed

src/assertions/application.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,11 @@ export function assertApplication(
2727
): asserts value is z.infer<typeof applicationSchema> {
2828
try {
2929
applicationSchema.parse(value);
30-
} catch {
30+
} catch (err) {
3131
throw new SquareCloudAPIError(
3232
"INVALID_API_APPLICATION",
3333
"Invalid application object received from API /apps/{app_id}",
34+
{ cause: err.errors },
3435
);
3536
}
3637
}
@@ -40,10 +41,11 @@ export function assertWebsiteApplication(
4041
): asserts value is z.infer<typeof websiteApplicationSchema> {
4142
try {
4243
websiteApplicationSchema.parse(value);
43-
} catch {
44+
} catch (err) {
4445
throw new SquareCloudAPIError(
4546
"INVALID_API_WEBSITE_APPLICATION",
4647
"Invalid website application object received from API /apps/{app_id}",
48+
{ cause: err.errors },
4749
);
4850
}
4951
}

src/assertions/status.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,11 @@ export function assertSimpleStatus(
4040
): asserts value is z.infer<typeof simpleStatusSchema> {
4141
try {
4242
simpleStatusSchema.parse(value);
43-
} catch {
43+
} catch (err) {
4444
throw new SquareCloudAPIError(
4545
"INVALID_API_STATUS_ALL",
4646
"Invalid simple status object received from API /apps/all/status",
47+
{ cause: err.errors },
4748
);
4849
}
4950
}
@@ -53,10 +54,11 @@ export function assertStatus(
5354
): asserts value is z.infer<typeof statusSchema> {
5455
try {
5556
statusSchema.parse(value);
56-
} catch {
57+
} catch (err) {
5758
throw new SquareCloudAPIError(
5859
"INVALID_API_STATUS",
5960
"Invalid status object received from API /apps/{app_id}/status",
61+
{ cause: err.errors },
6062
);
6163
}
6264
}

src/assertions/user.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,11 @@ export function assertUserInfo(
4242
): asserts value is z.infer<typeof userInfoSchema> {
4343
try {
4444
userInfoSchema.parse(value);
45-
} catch {
45+
} catch (err) {
4646
throw new SquareCloudAPIError(
4747
"INVALID_API_USER_INFO",
4848
"Invalid user info object received from API /user",
49+
{ cause: err.errors },
4950
);
5051
}
5152
}

src/structures/error.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
export class SquareCloudAPIError extends TypeError {
2-
constructor(code: string, message?: string) {
2+
constructor(
3+
code: string,
4+
message?: string,
5+
options?: { stack?: string; cause?: unknown },
6+
) {
37
super(code);
48

59
this.name = "SquareCloudAPIError";
@@ -10,5 +14,13 @@ export class SquareCloudAPIError extends TypeError {
1014
.toLowerCase()
1115
.replace(/(^|\s)\S/g, (L) => L.toUpperCase()) || "UNKNOWN_CODE") +
1216
(message ? `: ${message}` : "");
17+
18+
if (options?.stack) {
19+
this.stack = options.stack;
20+
}
21+
22+
if (options?.cause) {
23+
this.cause = options.cause;
24+
}
1325
}
1426
}

0 commit comments

Comments
 (0)