gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 11:56:34 24 4
gpt4 key购买 nike

我目前正在调试一些用 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com