3

I am having an application that queries PostgreSQL database with an excellent pg-promise library.

Is there a way to get a friendly stack trace in the case of query syntax contains errors? Either through Node configuration or pg-promise configuration.

  • The thrown UnhandledPromiseRejectionWarning on the query error does not give an indication wherein the source code the application made the query
  • I do not want to decorate all queries with individual try catches just to pinpoint the invalid query

Here is an example unhelpful stack trace:

(node:53357) UnhandledPromiseRejectionWarning: error: there is no parameter $1
    at Parser.parseErrorMessage (/Users/moo/code/myapp/node_modules/pg-protocol/dist/parser.js:278:15)
    at Parser.handlePacket (/Users/moo/code/myapp/node_modules/pg-protocol/dist/parser.js:126:29)
    at Parser.parse (/Users/moo/code/myapp/node_modules/pg-protocol/dist/parser.js:39:38)
    at Socket.<anonymous> (/Users/moo/code/myapp/node_modules/pg-protocol/dist/index.js:8:42)
    at Socket.emit (events.js:314:20)
    at addChunk (_stream_readable.js:303:12)
    at readableAddChunk (_stream_readable.js:279:9)
    at Socket.Readable.push (_stream_readable.js:218:10)
    at TCP.onStreamRead (internal/stream_base_commons.js:188:23)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:53357) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 2)
(node:53357) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

There is no indication where the query was made in this stack trace, making pinpointing bugs hard. Furthermore adding node --trace-warnings does not add more useful context to the stack trace.

Alternatively, are there any other PostgreSQL connector libraries for Node that would have more error tracking capabilities?

Node v14.

Mikko Ohtamaa
  • 82,057
  • 50
  • 264
  • 435

1 Answers1

4

Native stack tracing for promises in NodeJS is somewhat lacking. That's why within pg-promise it is documented everywhere that Bluebird with Long Stack Tracing is a better approach.

const Promise = require('bluebird'); // overload for the default Promise

Promise.config({
    longStackTraces: true // enable long-stack tracing
});

const pgp = require('pg-promise')({
    promiseLib: Promise // use custom promise library within pg-promise
});

Note that longStackTraces: true brings in a bit of performance penalty, so you do not want to enable it for production.

vitaly-t
  • 24,279
  • 15
  • 116
  • 138
  • Thank you for a wonderful library Vitaly! Hopefully Node.js developers figure out how to do stack traces nicely, as no other high-level programming language have this problem. – Mikko Ohtamaa Jul 24 '20 at 19:22
  • @MikkoOhtamaa Other high-level programming languages do not allow custom Promise implementations. That's why it is not a big deal within NodeJS, you can just opt for a library that does support it better. – vitaly-t Jul 25 '20 at 12:32
  • I understand. However this is a killer feature for a developer friendliness, so I hope Node.js would support it out of the box, so that novice devices do not need to go to Google to look for the solution which they might not know to exist in the first place :) – Mikko Ohtamaa Jul 27 '20 at 07:27