使用 pg-promise 对查询错误进行友好的堆栈跟踪

Friendly stack traces with pg-promise for query errors

我有一个应用程序可以使用出色的 pg-promise 库查询 PostgreSQL 数据库。

有没有办法在查询语法包含错误的情况下获得友好的堆栈跟踪?通过节点配置或 pg-promise 配置。

这是一个无用的堆栈跟踪示例:

(node:53357) UnhandledPromiseRejectionWarning: error: there is no parameter 
    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.

在此堆栈跟踪中没有指示查询是在何处进行的,因此很难查明错误。此外,添加 node --trace-warnings 不会向堆栈跟踪添加更多有用的上下文。

或者,是否有任何其他适用于 Node 的 PostgreSQL 连接器库具有更多错误跟踪功能?

节点 v14.

NodeJS 中的 promises 原生堆栈跟踪有些欠缺。这就是为什么在 pg-promise it is documented everywhere that Bluebird with Long Stack Tracing 内是更好的方法。

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
});

请注意 longStackTraces: true 会带来一些性能损失,因此您不想在生产环境中启用它。