Skip to content

Conversation

@mandarini
Copy link
Contributor

Summary

Fixes fetch error handling in postgrest-js to properly bubble up underlying error causes (DNS failures, network issues, timeouts), enabling informed retry logic in middleware and applications.

Problem

Previously, when fetch failed due to network issues, postgrest-js would return a generic error:

  {
    error: {
      message: "TypeError: fetch failed",
      details: "TypeError: fetch failed\n    at ...",
      hint: "",
      code: ""
    }
  }

The underlying cause (e.g., DNS resolution failure with ENOTFOUND) was lost, making it impossible to:

  • Distinguish between different types of network failures
  • Implement idempotent retry logic based on error type
  • Debug production issues effectively

Solution

Modified error handling in PostgrestBuilder.ts to extract and expose the cause property from fetch errors:

  {
    error: {
      message: "TypeError: fetch failed",
      details: "TypeError: fetch failed\n    ...\n\nCaused by: Error: getaddrinfo 
  ENOTFOUND example.com\n...\nError code: ENOTFOUND",
      hint: "Underlying cause: getaddrinfo ENOTFOUND example.com",
      code: "ENOTFOUND"  // ← Now populated!
    }
  }

Changes

  • PostgrestBuilder.ts: Enhanced fetch error handling to capture cause information
  • fetch-errors.test.ts: Added comprehensive test coverage for:
    • DNS resolution failures (ENOTFOUND)
    • Connection refused errors (ECONNREFUSED)
    • Timeout errors (ETIMEDOUT)
    • Errors with and without causes
    • Behavior with throwOnError()

@mandarini mandarini self-assigned this Nov 12, 2025
@github-actions github-actions bot added the postgrest-js Related to the postgrest-js library. label Nov 12, 2025
@mandarini mandarini requested a review from kevcodez November 12, 2025 12:19
@mandarini mandarini force-pushed the fix/postgrest-js-bubble-up-fetch-error-causes branch from 670b7d4 to c1bb5f0 Compare November 12, 2025 12:23
Copy link
Member

@steve-chavez steve-chavez left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

     error: {
      message: "TypeError: fetch failed",
      details: "TypeError: fetch failed\n    ...\n\nCaused by: Error: getaddrinfo 
  ENOTFOUND example.com\n...\nError code: ENOTFOUND",
      hint: "Underlying cause: getaddrinfo ENOTFOUND example.com",
      code: "ENOTFOUND"  // ← Now populated!
    }

Some problems with the above:

  • hint is not really a "hint" for the user.
  • code is meant to represent the upstream service error code, conflating it with client error codes can lead to confusion.

I would suggest not modifying hint and code, adding more content to details looks enough. Maybe TypeError: fetch failed\n ...\n\n from details can be removed?

@mandarini mandarini marked this pull request as ready for review November 12, 2025 15:41
@mandarini mandarini requested review from a team as code owners November 12, 2025 15:41
@pkg-pr-new
Copy link

pkg-pr-new bot commented Nov 12, 2025

Open in StackBlitz

@supabase/auth-js

npm i https://pkg.pr.new/@supabase/auth-js@1856

@supabase/functions-js

npm i https://pkg.pr.new/@supabase/functions-js@1856

@supabase/postgrest-js

npm i https://pkg.pr.new/@supabase/postgrest-js@1856

@supabase/realtime-js

npm i https://pkg.pr.new/@supabase/realtime-js@1856

@supabase/storage-js

npm i https://pkg.pr.new/@supabase/storage-js@1856

@supabase/supabase-js

npm i https://pkg.pr.new/@supabase/supabase-js@1856

commit: be21e84

@mandarini mandarini merged commit d7beb57 into master Nov 12, 2025
26 of 27 checks passed
@mandarini mandarini deleted the fix/postgrest-js-bubble-up-fetch-error-causes branch November 12, 2025 16:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

postgrest-js Related to the postgrest-js library. trigger: preview

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants