supabase / postgrest-js

Isomorphic JavaScript client for PostgREST.
https://supabase.com
MIT License
962 stars 128 forks source link

Errors are missing stack trace #501

Closed meyer9 closed 5 months ago

meyer9 commented 7 months ago

Bug report

Describe the bug

Errors returned do not include a stack trace because they are throwing a plain object instead of an Error. This makes it annoying to track down the root cause of an error because there's no way to tell where it was thrown from.

To Reproduce

Write a postgrest-js query that returns an error. Notice that error.stack is undefined and error instanceof Error is false.

import { PostgrestClient } from 'https://esm.sh/@supabase/postgrest-js@1.8.5';

const REST_URL = 'http://localhost:54321/rest/v1';
const postgrest = new PostgrestClient(REST_URL);

try {
  const { data } = await postgrest
    .from('does_not_exist')
    .select('*')
    .throwOnError();

  console.log(`Got data: ${data}`);
} catch (err) {
  console.log(err);
  console.log(err.stack);
  console.log(err instanceof Error);
}

// {
//   code: "42P01",
//   details: null,
//   hint: null,
//   message: 'relation "public.does_not_exist" does not exist'
// }
// undefined
// false

Expected behavior

I expect stack to be set on the result. I think it might be good to return a PostgrestError that extends Error with the error details, but also just throwing a plain Error is probably fine too!

meyer9 commented 7 months ago

Is there any chance I can get an update on this issue? It would be extremely useful to have a stack trace on errors thrown by postgrest-js for observability. Let me know if I can do anything to make it easier for maintainers!

meyer9 commented 5 months ago

@soedirgo @laurenceisla Please check this out. This is a huge issue for observability with a super simple fix. We currently can't trace DB errors down to a specific line. I have a PR up with tests.