Skip to content

Commit 125ff47

Browse files
committed
Preserve error context in JSON parsing error handlers
Add { cause: e } to error constructors in getResponseJson to preserve original error context for debugging. This follows the "no silent failures" principle by maintaining the full error chain when wrapping SyntaxError and unknown JSON parsing errors.
1 parent b7d5899 commit 125ff47

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

src/http-client.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@ export async function getResponseJson(
205205
// Attach the original response text for better error reporting.
206206
const enhancedError = new Error(
207207
`Socket API - Invalid JSON response:\n${responseBody}\n→ ${e.message}`,
208+
{ cause: e },
208209
) as SyntaxError & {
209210
originalResponse?: string
210211
}
@@ -218,9 +219,9 @@ export async function getResponseJson(
218219
throw e
219220
}
220221
// Handle non-Error objects thrown by JSON parsing.
221-
const unknownError = new Error(
222-
'Unknown JSON parsing error',
223-
) as SyntaxError & {
222+
const unknownError = new Error('Unknown JSON parsing error', {
223+
cause: e,
224+
}) as SyntaxError & {
224225
originalResponse?: string
225226
}
226227
unknownError.name = 'SyntaxError'

0 commit comments

Comments
 (0)