@@ -2,7 +2,7 @@ import { PostgrestClient } from '../src/index'
22import { Database } from './types.override'
33
44describe ( 'Fetch error handling' , ( ) => {
5- test ( 'should bubble up DNS error code (ENOTFOUND or EAI_AGAIN) from fetch cause ' , async ( ) => {
5+ test ( 'should bubble up DNS error cause in details ' , async ( ) => {
66 // Create a client with an invalid domain that will trigger DNS resolution error
77 const postgrest = new PostgrestClient < Database > (
88 'https://invalid-domain-that-does-not-exist.local'
@@ -15,21 +15,20 @@ describe('Fetch error handling', () => {
1515 expect ( res . status ) . toBe ( 0 )
1616 expect ( res . statusText ) . toBe ( '' )
1717
18- // The error code should be a DNS error code from the cause
19- // Different environments return different DNS error codes:
20- // - ENOTFOUND: Domain doesn't exist (most common)
21- // - EAI_AGAIN: Temporary DNS failure (common in CI)
22- expect ( [ 'ENOTFOUND' , 'EAI_AGAIN' ] ) . toContain ( res . error ! . code )
18+ // Client-side network errors don't populate code/hint (those are for upstream service errors)
19+ expect ( res . error ! . code ) . toBe ( '' )
20+ expect ( res . error ! . hint ) . toBe ( '' )
2321
24- // The message should still contain the fetch error
22+ // The message should contain the fetch error
2523 expect ( res . error ! . message ) . toContain ( 'fetch failed' )
2624
27- // The details should contain cause information
25+ // The details should contain cause information with error code
26+ // Different environments return different DNS error codes:
27+ // - ENOTFOUND: Domain doesn't exist (most common)
28+ // - EAI_AGAIN: Temporary DNS failure (common in CI)
2829 expect ( res . error ! . details ) . toContain ( 'Caused by:' )
29- expect ( res . error ! . details ) . toMatch ( / E N O T F O U N D | E A I _ A G A I N / )
30-
31- // The hint should contain the underlying cause message with getaddrinfo
32- expect ( res . error ! . hint ) . toContain ( 'getaddrinfo' )
30+ expect ( res . error ! . details ) . toContain ( 'getaddrinfo' )
31+ expect ( res . error ! . details ) . toMatch ( / \( E N O T F O U N D \) | \( E A I _ A G A I N \) / )
3332 } )
3433
3534 test ( 'should handle network errors with custom fetch implementation' , async ( ) => {
@@ -52,12 +51,12 @@ describe('Fetch error handling', () => {
5251 const res = await postgrest . from ( 'users' ) . select ( )
5352
5453 expect ( res . error ) . toBeTruthy ( )
55- expect ( res . error ! . code ) . toBe ( 'ENOTFOUND' )
54+ expect ( res . error ! . code ) . toBe ( '' )
55+ expect ( res . error ! . hint ) . toBe ( '' )
5656 expect ( res . error ! . message ) . toBe ( 'TypeError: fetch failed' )
5757 expect ( res . error ! . details ) . toContain ( 'Caused by:' )
5858 expect ( res . error ! . details ) . toContain ( 'getaddrinfo ENOTFOUND example.com' )
59- expect ( res . error ! . details ) . toContain ( 'Error code: ENOTFOUND' )
60- expect ( res . error ! . hint ) . toContain ( 'getaddrinfo ENOTFOUND example.com' )
59+ expect ( res . error ! . details ) . toContain ( '(ENOTFOUND)' )
6160 } )
6261
6362 test ( 'should handle connection refused errors' , async ( ) => {
@@ -81,9 +80,10 @@ describe('Fetch error handling', () => {
8180 const res = await postgrest . from ( 'users' ) . select ( )
8281
8382 expect ( res . error ) . toBeTruthy ( )
84- expect ( res . error ! . code ) . toBe ( 'ECONNREFUSED' )
83+ expect ( res . error ! . code ) . toBe ( '' )
84+ expect ( res . error ! . hint ) . toBe ( '' )
8585 expect ( res . error ! . details ) . toContain ( 'connect ECONNREFUSED' )
86- expect ( res . error ! . hint ) . toContain ( 'connect ECONNREFUSED' )
86+ expect ( res . error ! . details ) . toContain ( '( ECONNREFUSED) ' )
8787 } )
8888
8989 test ( 'should handle timeout errors' , async ( ) => {
@@ -105,8 +105,10 @@ describe('Fetch error handling', () => {
105105 const res = await postgrest . from ( 'users' ) . select ( )
106106
107107 expect ( res . error ) . toBeTruthy ( )
108- expect ( res . error ! . code ) . toBe ( 'ETIMEDOUT' )
108+ expect ( res . error ! . code ) . toBe ( '' )
109+ expect ( res . error ! . hint ) . toBe ( '' )
109110 expect ( res . error ! . details ) . toContain ( 'request timeout' )
111+ expect ( res . error ! . details ) . toContain ( '(ETIMEDOUT)' )
110112 } )
111113
112114 test ( 'should handle fetch errors without cause gracefully' , async ( ) => {
@@ -124,9 +126,11 @@ describe('Fetch error handling', () => {
124126 const res = await postgrest . from ( 'users' ) . select ( )
125127
126128 expect ( res . error ) . toBeTruthy ( )
127- expect ( res . error ! . code ) . toBe ( 'FETCH_ERROR' )
128- expect ( res . error ! . message ) . toBe ( 'TypeError: fetch failed' )
129+ expect ( res . error ! . code ) . toBe ( '' )
129130 expect ( res . error ! . hint ) . toBe ( '' )
131+ expect ( res . error ! . message ) . toBe ( 'TypeError: fetch failed' )
132+ // When no cause, details should still have the stack trace
133+ expect ( res . error ! . details ) . toBeTruthy ( )
130134 } )
131135
132136 test ( 'should handle generic errors without code' , async ( ) => {
@@ -141,6 +145,7 @@ describe('Fetch error handling', () => {
141145
142146 expect ( res . error ) . toBeTruthy ( )
143147 expect ( res . error ! . code ) . toBe ( '' )
148+ expect ( res . error ! . hint ) . toBe ( '' )
144149 expect ( res . error ! . message ) . toBe ( 'Error: Something went wrong' )
145150 } )
146151
0 commit comments