Search code examples
node.jspostgresqlpg-promise

Friendly stack traces with pg-promise for query errors


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.


Solution

  • 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.