javascript - 从 Jest 中获取正确的堆栈跟踪

标签 javascript typescript postgresql jestjs stack-trace

我目前正在调试一些用 jest over typescript 编写的测试,我有点头疼。
如果测试或测试类运行 Postgres SQL 并且查询中有错误,我会得到错误的堆栈跟踪,例如:

error: invalid input syntax for type integer: ""0""
    at Parser.parseErrorMessage (/Users/sklivvz/src/xxx/node_modules/pg-protocol/src/parser.ts:369:69)
    at Parser.handlePacket (/Users/sklivvz/src/xxx/node_modules/pg-protocol/src/parser.ts:188:21)
    at Parser.parse (/Users/sklivvz/src/xxx/node_modules/pg-protocol/src/parser.ts:103:30)
    at Socket.<anonymous> (/Users/sklivvz/src/xxx/node_modules/pg-protocol/src/index.ts:7:48)
    at Socket.emit (node:events:365:28)
    at addChunk (node:internal/streams/readable:314:12)
    at readableAddChunk (node:internal/streams/readable:289:9)
    at Socket.Readable.push (node:internal/streams/readable:228:10)
    at TCP.onStreamRead (node:internal/stream_base_commons:190:23)
“错误”行非常有用,但是,堆栈跟踪只告诉我错误是由 pg-protocol 驱动程序引发的。我想知道我的代码中的哪一行产生了错误。
我有 82.7% 的把握确定这是由于 PG 的 query是异步的。
不得不单步调试或(喘气)console.log 非常耗时。我解决每个错误的方法只是显示正确的调用堆栈以使其变得更好。
有没有人找到让这个开发人员友好的方法?

最佳答案

检查这是否与 brianc/node-postgres issue 2484 有关

is (there) a preferred package, extension, or method for providing more detail when you get a syntax error back from the parser?
(for instance, one that listed line number, column of the error)

for instance, right now:

error: syntax error at or near "as"
   at Parser.parseErrorMessage (/home/collspec/projects/staff-portal/sprint-server/node_modules/pg-protocol/dist/parser.js:278:15)

desired behavior:

error: syntax error at or near "as", line 5, column 7
   at Parser.parseErrorMessage (/home/collspec/projects/staff-portal/sprint-server/node_modules/pg-protocol/dist/parser.js:278:15)

Possible workaround从那个问题:

There are a bunch of additional fields on Error objects populated by the driver.
If you log the error object you can see them. They correspond to the error fields returned by the server:

For example with the command:

SELECT foo
FROM bar

You can get an error like this:

{
 length: 102,
 severity: 'ERROR',
 code: '42P01',
 detail: undefined,
 hint: undefined,
 position: '17',
 internalPosition: undefined,
 internalQuery: undefined,
 where: undefined,
 schema: undefined,
 table: undefined,
 column: undefined,
 dataType: undefined,
 constraint: undefined,
 file: 'parse_relation.c',
 line: '1180',
 routine: 'parserOpenTable'
}

The one you want is position. It gives you the character offset in the SQL of the error.
In this example the position value of "17" refers to the start of the bar token in the SQL.
It's not always populated though as it depends on what caused the error (generally just parse errors).

关于javascript - 从 Jest 中获取正确的堆栈跟踪,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68457787/

相关文章:

python - 将相同的 Pandas 数据帧存储到数据库后不相等

javascript - 从 Azure IotHub/EventHub 获取 deviceId

javascript - 在 JavaScript 中,如果在 HTML 元素及其祖先上声明事件触发器,哪个先触发?

javascript - JQuery 使用通配符 (*) 选择元素

angular - 从父级获取子级中的变量,从父组件触发它

postgresql - 如何使普通 linux 用户无需 sudo 访问权限即可访问 postgres 数据库?

Javascript 确认服务器上列表上的消息

angular - 在 http 解析中使用接口(interface)和 Observable 时出错

typescript - 在 TypeScript 中返回一个符号

mysql - 从 MySQL 迁移到具有 unicode 缺陷的 Postgres