Skip to content

Commit 47fdede

Browse files
authored
feat: allow skipping retries on specific pathnames (supabase#39894)
* feat: allow skipping retries on specific pathnames * remove system route
1 parent 1af6b84 commit 47fdede

File tree

3 files changed

+37
-3
lines changed

3 files changed

+37
-3
lines changed

apps/studio/data/fetchers.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,15 @@ client.use(
105105

106106
// add code field to body
107107
body.code = response.status
108+
108109
body.requestId = request.headers.get('X-Request-Id')
110+
109111
const retryAfterHeader = response.headers.get('Retry-After')
110112
body.retryAfter = retryAfterHeader ? parseInt(retryAfterHeader) : undefined
111113

114+
const requestUrl = new URL(request.url)
115+
body.requestPathname = requestUrl.pathname
116+
112117
return new Response(JSON.stringify(body), {
113118
headers: response.headers,
114119
status: response.status,
@@ -156,9 +161,13 @@ export const handleError = (error: unknown, options: HandleErrorOptions = {}): n
156161
'requestId' in error && typeof error.requestId === 'string' ? error.requestId : undefined
157162
const retryAfter =
158163
'retryAfter' in error && typeof error.retryAfter === 'number' ? error.retryAfter : undefined
164+
const requestPathname =
165+
'requestPathname' in error && typeof error.requestPathname === 'string'
166+
? error.requestPathname
167+
: undefined
159168

160169
if (errorMessage) {
161-
throw new ResponseError(errorMessage, errorCode, requestId, retryAfter)
170+
throw new ResponseError(errorMessage, errorCode, requestId, retryAfter, requestPathname)
162171
}
163172
}
164173

apps/studio/data/query-client.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { QueryClient, onlineManager } from '@tanstack/react-query'
2-
import { IS_PLATFORM } from 'lib/constants'
2+
import { match } from 'path-to-regexp'
33
import { useState } from 'react'
4+
5+
import { IS_PLATFORM } from 'lib/constants'
46
import { ResponseError } from 'types'
57

68
// When running locally we don't need the internet
@@ -9,6 +11,13 @@ if (!IS_PLATFORM) {
911
onlineManager.setOnline(true)
1012
}
1113

14+
const SKIP_RETRY_PATHNAME_MATCHERS = [
15+
'/platform/projects/:ref/run-lints',
16+
'/platform/organizations/:slug/usage',
17+
'/platform/pg-meta/:ref/query',
18+
'/v1/projects/:ref/analytics/endpoints/logs.all',
19+
].map((pathname) => match(pathname))
20+
1221
let queryClient: QueryClient | undefined
1322

1423
export function getQueryClient() {
@@ -31,6 +40,14 @@ export function getQueryClient() {
3140
return false
3241
}
3342

43+
if (
44+
error instanceof ResponseError &&
45+
error.requestPathname &&
46+
SKIP_RETRY_PATHNAME_MATCHERS.some((matchFn) => matchFn(error.requestPathname!))
47+
) {
48+
return false
49+
}
50+
3451
if (failureCount < 3) {
3552
return true
3653
}

apps/studio/types/base.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,12 +89,20 @@ export class ResponseError extends Error {
8989
code?: number
9090
requestId?: string
9191
retryAfter?: number
92+
requestPathname?: string
9293

93-
constructor(message: string | undefined, code?: number, requestId?: string, retryAfter?: number) {
94+
constructor(
95+
message: string | undefined,
96+
code?: number,
97+
requestId?: string,
98+
retryAfter?: number,
99+
requestPathname?: string
100+
) {
94101
super(message || 'API error happened while trying to communicate with the server.')
95102
this.code = code
96103
this.requestId = requestId
97104
this.retryAfter = retryAfter
105+
this.requestPathname = requestPathname
98106
}
99107
}
100108

0 commit comments

Comments
 (0)