Skip to content

Conversation

@voidfuck
Copy link

Problem

When the facilitator service returns non-JSON responses (such as HTML error pages, plain text, or malformed JSON), the application crashes with unclear error messages like Unexpected token or Unexpected end of JSON input.

This makes it difficult for developers to debug integration issues, especially when the facilitator server is experiencing errors or returning unexpected content types.

Solution

Added try-catch blocks around all res.json() calls in the useFacilitator functions:

  • verify()
  • settle()
  • supported()
  • list()

These blocks provide descriptive error messages that include:

  • The operation that failed (verify/settle/supported/list)
  • The original error message
  • The HTTP status code for context

Example

Before:

SyntaxError: Unexpected token at position 0

After:

Error: Failed to parse verify response as JSON: Unexpected token at position 0. Response status: 200

Impact

  • Better debugging experience for developers
  • More resilient to unexpected server responses
  • Follows error handling best practices
  • No breaking changes to the API

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant