Urgent.News

What's breaking now, across hundreds of outlets.

Tech

Debug HTTP 422 API Validation Errors Safely

When an API returns an HTTP 422 response, client integrations often discard field-level details or dump entire response bodies into logs. Quick solution: async function readAndFormat422 ( response : Response ): Promise < string > { const raw = await response . text (); try { const body = JSON . parse ( raw ); const errors = body . errors ; if ( errors && typeof errors === " object " ) { return…

When an API returns an HTTP 422 response, client integrations often overlook field-level details or flood logs with the entire response body. To handle these errors safely, you can use an asynchronous function called "readAndFormat422" that takes a response object and returns a promise string. The function reads the response text, attempts to parse it as JSON, and extracts the first ten error fields if successful.

If parsing fails, it returns the first 200 characters of the raw response. HTTP 422 status codes indicate that the server understands the request content type and syntax, but cannot process the contained instructions. This status code requires your client to handle varied error-body schemas safely without assuming a universal structure.

Avoid the anti-patterns of only catching non-2xx responses or logging raw response text, as these can lead to loss of valuable context and sensitive data leakage. Instead, bound character lengths and field counts, sanitize sensitive patterns before persistence, and confirm that 422 responses do not trigger infinite retry loops.

Rewritten by urgent.news; may contain errors — check the original.

Source: dev.to

Read the original at dev.to →

Report a problem with this story

More in Tech

Custom Domain Transactional Email API Beats Unified Messaging for 1 Realtime Receipt

A NodeJS API sending transactional welcome email and payment receipts from a custom domain has one decisive constraint: how quickly must the support stack react to delivery trouble?

  • Dedicated email API preferred for real-time receipt automation
  • Unified REST surface like Infrai sufficient for polling-based receipt handling
  • Security measures include secrets, packages, and API key protection

More from Wednesday, 14 October 2026 →