neondatabase / serverless

Connect to Neon PostgreSQL from serverless/worker/edge functions
https://www.npmjs.com/package/@neondatabase/serverless
MIT License
318 stars 11 forks source link

`NeonDbError` does not preserve a stack trace when it is thrown #82

Open brettimus opened 1 month ago

brettimus commented 1 month ago

Steps to reproduce

Expected result

The NeonDbError should include a stack trace whenever possible

Actual result

Instances of NeonDbError do not include a stack trace

Environment

Cloudflare workers (but also everywhere)

Logs, links

The stack trace of an error is helpful for error monitoring tools, to be able to link back to the line of code that triggered the error.

As of writing, NeonDbError does indeed inherit from Error, but it does not include a stack trace, as Error usually does.

This would be possible to implement with a quick code change, adding a constructor to the custom NeonDbError class:

  constructor(message: string) {
    super(message);
    if (Error.captureStackTrace) {
      Error.captureStackTrace(this, BugError);
    }
  }

I'm not sure of serverless environment support for Error.captureStackTrace, since this is not a web standard, per se. However, it does exist for Cloudflare Workers. That's why there's a conditional check for Error.captureStackTrace.

I'm willing to pick this up in a PR if you all are open to it!! Would be more than happy to contribute.

brettimus commented 1 month ago

I put up a PR in case this is something you all would like to move ahead with! https://github.com/neondatabase/serverless/pull/83