Better stack traces #829
Replies: 5 comments 4 replies
-
Also wondering what the current best practice is for this in 2022, with Node 16 as current stable version? To be honest, if the error would include the full query it is trying to parse that would be very helpful in hunting down which query is failing. Do you think this is possible @vitaly-t? [1] I went through the previous issues on the topic: Seems like bluebird is always the recommended approach, but the github of bluebird states to use native promises if possible: https://github.com/petkaantonov/bluebird#%EF%B8%8Fnote%EF%B8%8F Sorry @vitaly-t you are probably sick of answering this question 😛 [1] Eg: (copying an example from previous issue) db.query('SELECT error').catch(err => console.error(err)); currently this would throw:
Would be cool if the error would include the query |
Beta Was this translation helpful? Give feedback.
-
Ignore what they say there, and continue to use Bluebird, their stack tracing is far superior than the native Promise: const Promise = require('bluebird');
const initOptions = {
promiseLib: Promise
};
const pgp = require('pg-promise')(initOptions); And then activate |
Beta Was this translation helpful? Give feedback.
-
@tomskopek The library provides custom errors only for specific situations. It does not handle server-side generic errors. But you have event error that reports query details: const initOptions = {
error(err, e) {
// here, you have r.query + e.params
}
}; |
Beta Was this translation helpful? Give feedback.
-
Yes. The db.none(() = {
// ....
return this.pgp.helpers.insert({ foo: 'bar' }, columnSet) + ' RETURNING id';
}); i.e. all query functions support a function as parameter, to return the query, and that will join the general error handling. |
Beta Was this translation helpful? Give feedback.
-
Since errors now include the query, it got me thinking if |
Beta Was this translation helpful? Give feedback.
-
Hi Vitaly, I have been struggling for a while with how some of the stack traces in errors caught from calls to different pg-promise functions, and I've gone as far as wrapping each call to the main
pgdb
client in as small of a try-catch as possible, but that still only gives me an approximation of where an error occurs, especially if thetry
block has to encapsulate a couple calls (like in a transaction). I think I've come up with a pretty good solution now, using proxies:https://codereview.stackexchange.com/questions/275071/proper-use-of-a-javascript-proxy-for-better-stack-traces-from-external-libraries
Because I'm using your library as an example, I thought maybe your eyes could see some better ways to approach this problem.
What do you think?
Beta Was this translation helpful? Give feedback.
All reactions