Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 32 additions & 8 deletions typescript/packages/x402/src/verify/useFacilitator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -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;
}

/**
Expand All @@ -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;
}

/**
Expand Down Expand Up @@ -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 };
Expand Down