Skip to content

Commit

Permalink
Rename the unwrap API to throwOnError instead
Browse files Browse the repository at this point in the history
The `unwrap` moniker was a bit of a misnomer, since it didn't actually
unwrap the result, and this should make things a bit more clear. This was
discussed in a bit more detail in #188.
  • Loading branch information
mstade committed Jun 11, 2021
1 parent 6209345 commit 3a61484
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 12 deletions.
10 changes: 5 additions & 5 deletions src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,20 +53,20 @@ export abstract class PostgrestBuilder<T> implements PromiseLike<PostgrestRespon
protected headers!: { [key: string]: string }
protected schema?: string
protected body?: Partial<T> | Partial<T>[]
protected unwrapError?: boolean
protected shouldThrowOnError?: boolean

constructor(builder: PostgrestBuilder<T>) {
Object.assign(this, builder)
}

/**
* If there's an error with the query, unwrap will reject the promise by
* If there's an error with the query, throwOnError will reject the promise by
* throwing the error instead of returning it as part of a successful response.
*
* {@link https://github.com/supabase/supabase-js/issues/92}
*/
unwrap(): PostgrestBuilder<T> {
this.unwrapError = true
throwOnError(): PostgrestBuilder<T> {
this.shouldThrowOnError = true
return this
}

Expand Down Expand Up @@ -115,7 +115,7 @@ export abstract class PostgrestBuilder<T> implements PromiseLike<PostgrestRespon
} else {
error = await res.json()

if (error && this.unwrapError) {
if (error && this.shouldThrowOnError) {
throw error
}
}
Expand Down
4 changes: 2 additions & 2 deletions test/__snapshots__/index.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -626,7 +626,7 @@ Object {
}
`;

exports[`connection error when unwrapping too 1`] = `[FetchError: request to http://this.url.does.not.exist/user?select=* failed, reason: getaddrinfo ENOTFOUND this.url.does.not.exist]`;
exports[`connection errors should work the same with throwOnError 1`] = `[FetchError: request to http://this.url.does.not.exist/user?select=* failed, reason: getaddrinfo ENOTFOUND this.url.does.not.exist]`;

exports[`don't mutate PostgrestClient.headers 1`] = `null`;

Expand Down Expand Up @@ -2254,7 +2254,7 @@ Object {
}
`;
exports[`unwrap throws errors instead of returning them 1`] = `
exports[`throwOnError throws errors instead of returning them 1`] = `
Object {
"code": "42P01",
"details": null,
Expand Down
10 changes: 5 additions & 5 deletions test/basic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,11 @@ test('missing table', async () => {
expect(res).toMatchSnapshot()
})

test('unwrap throws errors instead of returning them', async () => {
test('throwOnError throws errors instead of returning them', async () => {
let isErrorCaught = false

try {
await postgrest.from('missing_table').select().unwrap()
await postgrest.from('missing_table').select().throwOnError()
} catch (error) {
expect(error).toMatchSnapshot()
isErrorCaught = true
Expand All @@ -120,14 +120,14 @@ test('connection error', async () => {
expect(isErrorCaught).toBe(true)
})

test('connection error when unwrapping too', async () => {
test('connection errors should work the same with throwOnError', async () => {
const postgrest = new PostgrestClient('http://this.url.does.not.exist')
let isErrorCaught = false
await postgrest
.from('user')
.select()
.unwrap()
.then(undefined, error => {
.throwOnError()
.then(undefined, (error) => {
expect(error).toMatchSnapshot()
isErrorCaught = true
})
Expand Down

0 comments on commit 3a61484

Please sign in to comment.