From 5e23f978d65cdde467547bad9c3bad6034adc940 Mon Sep 17 00:00:00 2001 From: voidfuck Date: Sat, 29 Nov 2025 01:59:09 +0800 Subject: [PATCH] Add error handling for JSON parsing in facilitator API calls When the facilitator service returns non-JSON responses (such as HTML error pages or plain text), the application would crash with unclear error messages. This change adds try-catch blocks around all JSON parsing operations in the useFacilitator functions to provide better error messages that include context about what failed and the HTTP status code. This improves the debugging experience for developers integrating x402 and makes the library more resilient to unexpected server responses. --- .../x402/src/verify/useFacilitator.ts | 40 +++++++++++++++---- 1 file changed, 32 insertions(+), 8 deletions(-) diff --git a/typescript/packages/x402/src/verify/useFacilitator.ts b/typescript/packages/x402/src/verify/useFacilitator.ts index 518f409..ca24554 100644 --- a/typescript/packages/x402/src/verify/useFacilitator.ts +++ b/typescript/packages/x402/src/verify/useFacilitator.ts @@ -61,8 +61,14 @@ export function useFacilitator(facilitator?: FacilitatorConfig) { throw new Error(`Failed to verify payment: ${res.statusText}`); } - const data = await res.json(); - return data as VerifyResponse; + let data: VerifyResponse; + try { + data = await res.json(); + } catch (error) { + const message = error instanceof Error ? error.message : String(error); + throw new Error(`Failed to parse verify response as JSON: ${message}. Response status: ${res.status}`); + } + return data; } /** @@ -99,8 +105,14 @@ export function useFacilitator(facilitator?: FacilitatorConfig) { throw new Error(`Failed to settle payment: ${res.status} ${text}`); } - const data = await res.json(); - return data as SettleResponse; + let data: SettleResponse; + try { + data = await res.json(); + } catch (error) { + const message = error instanceof Error ? error.message : String(error); + throw new Error(`Failed to parse settle response as JSON: ${message}. Response status: ${res.status}`); + } + return data; } /** @@ -126,8 +138,14 @@ export function useFacilitator(facilitator?: FacilitatorConfig) { throw new Error(`Failed to get supported payment kinds: ${res.statusText}`); } - const data = await res.json(); - return data as SupportedPaymentKindsResponse; + let data: SupportedPaymentKindsResponse; + try { + data = await res.json(); + } catch (error) { + const message = error instanceof Error ? error.message : String(error); + throw new Error(`Failed to parse supported payment kinds response as JSON: ${message}. Response status: ${res.status}`); + } + return data; } /** @@ -165,8 +183,14 @@ export function useFacilitator(facilitator?: FacilitatorConfig) { throw new Error(`Failed to list discovery: ${res.status} ${text}`); } - const data = await res.json(); - return data as ListDiscoveryResourcesResponse; + let data: ListDiscoveryResourcesResponse; + try { + data = await res.json(); + } catch (error) { + const message = error instanceof Error ? error.message : String(error); + throw new Error(`Failed to parse discovery list response as JSON: ${message}. Response status: ${res.status}`); + } + return data; } return { verify, settle, supported, list };