Skip to content

Commit d0f3372

Browse files
authored
fix: capture error cause in serialisation (#727)
1 parent 0add8e0 commit d0f3372

2 files changed

Lines changed: 24 additions & 6 deletions

File tree

packages/synapse-core/src/errors/base.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,31 @@ export class SynapseError extends Error {
4242
this.shortMessage = message
4343
}
4444

45+
toJSON(): Record<string, unknown> {
46+
return {
47+
name: this.name,
48+
message: this.message,
49+
...(this.details ? { details: this.details } : {}),
50+
...(this.cause ? { cause: serializeErrorCause(this.cause) } : {}),
51+
}
52+
}
53+
4554
static is(value: unknown): value is SynapseError {
4655
return isSynapseError(value) && value.name === 'SynapseError'
4756
}
4857
}
4958

59+
function serializeErrorCause(error: Error, depth: number = 0): Record<string, unknown> {
60+
if (depth > 4) {
61+
return { name: error.name, message: error.message }
62+
}
63+
return {
64+
name: error.name,
65+
message: error.message,
66+
...(error.cause instanceof Error ? { cause: serializeErrorCause(error.cause, depth + 1) } : {}),
67+
}
68+
}
69+
5070
/**
5171
* Validation error thrown when a value does not match the expected Zod schema.
5272
*/

packages/synapse-sdk/src/errors/storage.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,9 @@ export class StoreError extends SynapseError {
2626
this.endpoint = options?.endpoint
2727
}
2828

29-
toJSON() {
29+
override toJSON() {
3030
return {
31-
name: this.name,
32-
message: this.message,
31+
...super.toJSON(),
3332
providerId: this.providerId,
3433
endpoint: this.endpoint,
3534
}
@@ -61,10 +60,9 @@ export class CommitError extends SynapseError {
6160
this.endpoint = options?.endpoint
6261
}
6362

64-
toJSON() {
63+
override toJSON() {
6564
return {
66-
name: this.name,
67-
message: this.message,
65+
...super.toJSON(),
6866
providerId: this.providerId,
6967
endpoint: this.endpoint,
7068
}

0 commit comments

Comments
 (0)