Skip to content
This repository was archived by the owner on Oct 9, 2025. It is now read-only.

Commit 70ed485

Browse files
committed
feat: support setting throwOnError at the client level
Currently, throwOnError can be set to true for any given query (/rpc call). This change allows the flag to be set to true at client initialization time, ensuring that you do not need to rely on each invocation either handling the error returned, or setting the flag correctly.
1 parent 5f47e84 commit 70ed485

File tree

6 files changed

+96
-8
lines changed

6 files changed

+96
-8
lines changed

src/PostgrestClient.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export default class PostgrestClient {
99
headers: { [key: string]: string }
1010
schema?: string
1111
fetch?: Fetch
12+
shouldThrowOnError?: boolean
1213

1314
/**
1415
* Creates a PostgREST client.
@@ -23,12 +24,19 @@ export default class PostgrestClient {
2324
headers = {},
2425
schema,
2526
fetch,
26-
}: { headers?: { [key: string]: string }; schema?: string; fetch?: Fetch } = {}
27+
throwOnError,
28+
}: {
29+
headers?: { [key: string]: string }
30+
schema?: string
31+
fetch?: Fetch
32+
throwOnError?: boolean
33+
} = {}
2734
) {
2835
this.url = url
2936
this.headers = { ...DEFAULT_HEADERS, ...headers }
3037
this.schema = schema
3138
this.fetch = fetch
39+
this.shouldThrowOnError = throwOnError
3240
}
3341

3442
/**
@@ -52,6 +60,7 @@ export default class PostgrestClient {
5260
headers: this.headers,
5361
schema: this.schema,
5462
fetch: this.fetch,
63+
shouldThrowOnError: this.shouldThrowOnError,
5564
})
5665
}
5766

@@ -79,6 +88,7 @@ export default class PostgrestClient {
7988
headers: this.headers,
8089
schema: this.schema,
8190
fetch: this.fetch,
91+
shouldThrowOnError: this.shouldThrowOnError,
8292
}).rpc(params, { head, count })
8393
}
8494
}

src/lib/PostgrestQueryBuilder.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,15 @@ export default class PostgrestQueryBuilder<T> extends PostgrestBuilder<T> {
88
headers = {},
99
schema,
1010
fetch,
11-
}: { headers?: { [key: string]: string }; schema?: string; fetch?: Fetch } = {}
11+
shouldThrowOnError,
12+
}: {
13+
headers?: { [key: string]: string }
14+
schema?: string
15+
fetch?: Fetch
16+
shouldThrowOnError?: boolean
17+
} = {}
1218
) {
13-
super(({ fetch } as unknown) as PostgrestBuilder<T>)
19+
super({ fetch, shouldThrowOnError } as unknown as PostgrestBuilder<T>)
1420
this.url = new URL(url)
1521
this.headers = { ...headers }
1622
this.schema = schema

src/lib/PostgrestRpcBuilder.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,15 @@ export default class PostgrestRpcBuilder<T> extends PostgrestBuilder<T> {
88
headers = {},
99
schema,
1010
fetch,
11-
}: { headers?: { [key: string]: string }; schema?: string; fetch?: Fetch } = {}
11+
shouldThrowOnError,
12+
}: {
13+
headers?: { [key: string]: string }
14+
schema?: string
15+
fetch?: Fetch
16+
shouldThrowOnError?: boolean
17+
} = {}
1218
) {
13-
super(({ fetch } as unknown) as PostgrestBuilder<T>)
19+
super({ fetch, shouldThrowOnError } as unknown as PostgrestBuilder<T>)
1420
this.url = new URL(url)
1521
this.headers = { ...headers }
1622
this.schema = schema

src/lib/types.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ export abstract class PostgrestBuilder<T> implements PromiseLike<PostgrestRespon
5656
protected headers!: { [key: string]: string }
5757
protected schema?: string
5858
protected body?: Partial<T> | Partial<T>[]
59-
protected shouldThrowOnError = false
59+
protected shouldThrowOnError: boolean
6060
protected signal?: AbortSignal
6161
protected fetch: Fetch
6262

@@ -71,6 +71,7 @@ export abstract class PostgrestBuilder<T> implements PromiseLike<PostgrestRespon
7171
_fetch = fetch
7272
}
7373
this.fetch = (...args) => _fetch(...args)
74+
this.shouldThrowOnError = builder.shouldThrowOnError || false
7475
}
7576

7677
/**
@@ -79,8 +80,11 @@ export abstract class PostgrestBuilder<T> implements PromiseLike<PostgrestRespon
7980
*
8081
* {@link https://github.com/supabase/supabase-js/issues/92}
8182
*/
82-
throwOnError(): PostgrestBuilder<T> {
83-
this.shouldThrowOnError = true
83+
throwOnError(throwOnError?: boolean): PostgrestBuilder<T> {
84+
if (throwOnError === null || throwOnError === undefined) {
85+
throwOnError = true
86+
}
87+
this.shouldThrowOnError = throwOnError
8488
return this
8589
}
8690

test/__snapshots__/index.test.ts.snap

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2285,6 +2285,31 @@ Object {
22852285
}
22862286
`;
22872287
2288+
exports[`throwOnError can be disabled per call 1`] = `
2289+
Object {
2290+
"code": "42P01",
2291+
"details": null,
2292+
"hint": null,
2293+
"message": "relation \\"public.missing_table\\" does not exist",
2294+
}
2295+
`;
2296+
2297+
exports[`throwOnError setting at the client level - query 1`] = `
2298+
Object {
2299+
"code": "42P01",
2300+
"details": null,
2301+
"hint": null,
2302+
"message": "relation \\"public.missing_table\\" does not exist",
2303+
}
2304+
`;
2305+
2306+
exports[`throwOnError setting at the client level - rpc 1`] = `
2307+
Object {
2308+
"hint": "If a new function was created in the database with this name and parameters, try reloading the schema cache.",
2309+
"message": "Could not find the public.missing_fn() function or the public.missing_fn function with a single unnamed json or jsonb parameter in the schema cache",
2310+
}
2311+
`;
2312+
22882313
exports[`throwOnError throws errors instead of returning them 1`] = `
22892314
Object {
22902315
"code": "42P01",

test/basic.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,43 @@ test('throwOnError throws errors instead of returning them', async () => {
142142
expect(isErrorCaught).toBe(true)
143143
})
144144

145+
test('throwOnError setting at the client level - query', async () => {
146+
let isErrorCaught = false
147+
const postgrest_ = new PostgrestClient(REST_URL, { throwOnError: true })
148+
149+
try {
150+
await postgrest_.from('missing_table').select()
151+
} catch (error) {
152+
expect(error).toMatchSnapshot()
153+
isErrorCaught = true
154+
}
155+
156+
expect(isErrorCaught).toBe(true)
157+
})
158+
159+
test('throwOnError setting at the client level - rpc', async () => {
160+
let isErrorCaught = false
161+
const postgrest_ = new PostgrestClient(REST_URL, { throwOnError: true })
162+
163+
try {
164+
await postgrest_.rpc('missing_fn').select()
165+
} catch (error) {
166+
expect(error).toMatchSnapshot()
167+
isErrorCaught = true
168+
}
169+
170+
expect(isErrorCaught).toBe(true)
171+
})
172+
173+
test('throwOnError can be disabled per call', async () => {
174+
let isErrorCaught = false
175+
const postgrest_ = new PostgrestClient(REST_URL, { throwOnError: true })
176+
const { error } = await postgrest_.from('missing_table').select().throwOnError(false)
177+
178+
expect(error).toMatchSnapshot()
179+
expect(isErrorCaught).toBe(false)
180+
})
181+
145182
test('connection error w/o throwing', async () => {
146183
const postgrest = new PostgrestClient('http://foo.invalid')
147184
let isErrorCaught = false

0 commit comments

Comments
 (0)