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 };