Skip to content

Commit d588ac8

Browse files
committed
fix(postgrest): handle 503/PGRST002, drain body before retry, rethrow AbortError
1 parent e31fb52 commit d588ac8

6 files changed

Lines changed: 723 additions & 577 deletions

File tree

‎packages/core/postgrest-js/src/PostgrestBuilder.ts‎

Lines changed: 15 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -154,10 +154,9 @@ export default abstract class PostgrestBuilder<
154154
return this
155155
}
156156

157-
/** *
158-
* @category Database
159-
*/
160157
/**
158+
* @category Database
159+
*
161160
* Configure retry behavior for this request.
162161
*
163162
* By default, retries are enabled for GET/HEAD requests that fail with
@@ -229,29 +228,6 @@ export default abstract class PostgrestBuilder<
229228
requestHeaders.set('X-Retry-Count', String(attemptCount))
230229
}
231230

232-
// Fix for https://github.com/supabase/postgrest-js/issues/361 — applies to all methods.
233-
if (this.isMaybeSingle && Array.isArray(data)) {
234-
if (data.length > 1) {
235-
error = {
236-
// https://github.com/PostgREST/postgrest/blob/a867d79c42419af16c18c3fb019eba8df992626f/src/PostgREST/Error.hs#L553
237-
code: 'PGRST116',
238-
details: `Results contain ${data.length} rows, application/vnd.pgrst.object+json requires 1 row`,
239-
hint: null,
240-
message: 'JSON object requested, multiple (or no) rows returned',
241-
}
242-
data = null
243-
count = null
244-
status = 406
245-
statusText = 'Not Acceptable'
246-
} else if (data.length === 1) {
247-
data = data[0]
248-
} else {
249-
data = null
250-
}
251-
}
252-
} else {
253-
const body = await res.text()
254-
255231
try {
256232
const res = await _fetch(this.url.toString(), {
257233
method: this.method,
@@ -262,7 +238,12 @@ export default abstract class PostgrestBuilder<
262238

263239
// Check if we should retry this response
264240
if (shouldRetry(this.method, res.status, attemptCount, this.retryEnabled)) {
265-
const delay = getRetryDelay(attemptCount)
241+
const retryAfterHeader = res.headers.get('Retry-After')
242+
const delay =
243+
retryAfterHeader !== null
244+
? Math.max(0, parseInt(retryAfterHeader, 10) || 0) * 1000
245+
: getRetryDelay(attemptCount)
246+
await res.body?.cancel()
266247
attemptCount++
267248
await sleep(delay)
268249
continue
@@ -271,13 +252,16 @@ export default abstract class PostgrestBuilder<
271252
// Process successful or final response
272253
return await this.processResponse(res)
273254
} catch (fetchError: any) {
255+
// Never retry aborted requests
256+
if (fetchError?.name === 'AbortError' || fetchError?.code === 'ABORT_ERR') {
257+
throw fetchError
258+
}
259+
274260
// Don't retry network errors for non-idempotent methods
275261
if (!RETRYABLE_METHODS.includes(this.method as (typeof RETRYABLE_METHODS)[number])) {
276262
throw fetchError
277263
}
278264

279-
if (error && this.shouldThrowOnError) {
280-
throw new PostgrestError(error)
281265
// Check if we should retry network errors
282266
if (this.retryEnabled && attemptCount < DEFAULT_MAX_RETRIES) {
283267
const delay = getRetryDelay(attemptCount)
@@ -404,9 +388,8 @@ export default abstract class PostgrestBuilder<
404388
count = parseInt(contentRange[1])
405389
}
406390

407-
// Temporary partial fix for https://github.com/supabase/postgrest-js/issues/361
408-
// Issue persists e.g. for `.insert([...]).select().maybeSingle()`
409-
if (this.isMaybeSingle && this.method === 'GET' && Array.isArray(data)) {
391+
// Fix for https://github.com/supabase/postgrest-js/issues/361 — applies to all methods.
392+
if (this.isMaybeSingle && Array.isArray(data)) {
410393
if (data.length > 1) {
411394
error = {
412395
// https://github.com/PostgREST/postgrest/blob/a867d79c42419af16c18c3fb019eba8df992626f/src/PostgREST/Error.hs#L553
@@ -450,12 +433,6 @@ export default abstract class PostgrestBuilder<
450433
}
451434
}
452435

453-
if (error && this.isMaybeSingle && error?.details?.includes('0 rows')) {
454-
error = null
455-
status = 200
456-
statusText = 'OK'
457-
}
458-
459436
if (error && this.shouldThrowOnError) {
460437
throw new PostgrestError(error)
461438
}

‎packages/core/postgrest-js/src/PostgrestClient.ts‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,6 @@ export default class PostgrestClient<
161161
} else {
162162
this.fetch = originalFetch
163163
}
164-
this.fetch = fetch
165164
this.retry = retry
166165
}
167166
from<

‎packages/core/postgrest-js/src/types/common/common.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@ export const getRetryDelay = (attemptIndex: number): number =>
2020
/**
2121
* Status codes that are safe to retry.
2222
* 520 = Cloudflare timeout/connection errors (transient)
23+
* 503 = PostgREST schema cache not yet loaded (transient, signals retry via Retry-After header)
2324
*/
24-
export const RETRYABLE_STATUS_CODES = [520] as const
25+
export const RETRYABLE_STATUS_CODES = [520, 503] as const
2526

2627
/**
2728
* HTTP methods that are safe to retry (idempotent operations).

0 commit comments

Comments
 (0)