From acc0d42297203dcd7de741696ea4d27b50cab8ec Mon Sep 17 00:00:00 2001 From: Julian Meyer Date: Wed, 8 Nov 2023 18:59:37 -0800 Subject: [PATCH 1/3] Ensure thrown errors have a stack trace --- src/PostgrestBuilder.ts | 3 ++- src/PostgrestError.ts | 8 ++++++++ test/basic.ts | 9 +++++++++ 3 files changed, 19 insertions(+), 1 deletion(-) create mode 100644 src/PostgrestError.ts diff --git a/src/PostgrestBuilder.ts b/src/PostgrestBuilder.ts index 23e397f1..96a8bc55 100644 --- a/src/PostgrestBuilder.ts +++ b/src/PostgrestBuilder.ts @@ -2,6 +2,7 @@ import nodeFetch from '@supabase/node-fetch' import type { Fetch, PostgrestSingleResponse } from './types' +import PostgrestError from './PostgrestError' export default abstract class PostgrestBuilder implements PromiseLike> @@ -156,7 +157,7 @@ export default abstract class PostgrestBuilder } if (error && this.shouldThrowOnError) { - throw error + throw new PostgrestError(error) } } diff --git a/src/PostgrestError.ts b/src/PostgrestError.ts new file mode 100644 index 00000000..e7d205af --- /dev/null +++ b/src/PostgrestError.ts @@ -0,0 +1,8 @@ +class PostgrestError extends Error { + constructor(details: { message?: string } & Record) { + super(details.message ?? 'Unknown Postgrest error') + Object.assign(this, details) + } +} + +export default PostgrestError diff --git a/test/basic.ts b/test/basic.ts index 0c948828..e8f4242e 100644 --- a/test/basic.ts +++ b/test/basic.ts @@ -645,6 +645,15 @@ test('throwOnError throws errors instead of returning them', async () => { expect(isErrorCaught).toBe(true) }) +test('throwOnError throws errors which include stack', async () => { + try { + const res = await postgrest.from('does_not_exist').select().throwOnError() + } catch (err) { + expect(err instanceof Error).toBe(true) + expect((err as Error).stack).not.toBeUndefined() + } +}) + // test('throwOnError setting at the client level - query', async () => { // let isErrorCaught = false // const postgrest_ = new PostgrestClient(REST_URL, { throwOnError: true }) From 0679c7359f93b7ba552e4cd3b91cc2948281aa61 Mon Sep 17 00:00:00 2001 From: Bobbie Soedirgo Date: Mon, 15 Jan 2024 13:11:11 +0800 Subject: [PATCH 2/3] chore: stricter typing --- src/PostgrestError.ts | 19 +++++++++++++------ test/basic.ts | 2 +- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/src/PostgrestError.ts b/src/PostgrestError.ts index e7d205af..8253ae96 100644 --- a/src/PostgrestError.ts +++ b/src/PostgrestError.ts @@ -1,8 +1,15 @@ -class PostgrestError extends Error { - constructor(details: { message?: string } & Record) { - super(details.message ?? 'Unknown Postgrest error') - Object.assign(this, details) +import type { PostgrestError as IPostgrestError } from './types' + +export default class PostgrestError extends Error implements IPostgrestError { + details: string + hint: string + code: string + + constructor(context: IPostgrestError) { + super(context.message) + this.name = 'PostgrestError' + this.details = context.details + this.hint = context.hint + this.code = context.code } } - -export default PostgrestError diff --git a/test/basic.ts b/test/basic.ts index e8f4242e..bd79de75 100644 --- a/test/basic.ts +++ b/test/basic.ts @@ -647,7 +647,7 @@ test('throwOnError throws errors instead of returning them', async () => { test('throwOnError throws errors which include stack', async () => { try { - const res = await postgrest.from('does_not_exist').select().throwOnError() + await postgrest.from('does_not_exist').select().throwOnError() } catch (err) { expect(err instanceof Error).toBe(true) expect((err as Error).stack).not.toBeUndefined() From 46c7e0039c94c6332d4c0c66ccc14e7a437cb5b6 Mon Sep 17 00:00:00 2001 From: Bobbie Soedirgo Date: Mon, 15 Jan 2024 13:14:02 +0800 Subject: [PATCH 3/3] chore: update snapshot --- test/basic.ts | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/test/basic.ts b/test/basic.ts index bd79de75..9a170019 100644 --- a/test/basic.ts +++ b/test/basic.ts @@ -631,14 +631,9 @@ test('throwOnError throws errors instead of returning them', async () => { try { await postgrest.from('missing_table').select().throwOnError() } catch (error) { - expect(error).toMatchInlineSnapshot(` - Object { - "code": "42P01", - "details": null, - "hint": null, - "message": "relation \\"public.missing_table\\" does not exist", - } - `) + expect(error).toMatchInlineSnapshot( + `[PostgrestError: relation "public.missing_table" does not exist]` + ) isErrorCaught = true }