(node:10716) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 2): Error: getaddrinfo ENOTFOUND
(node:10716) 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.
I'm using the wrong host to test error handling, and this came up.
How can I avoid that? I'm using Node 7 Async/Await like this:
async function selecionarFeriadoPorId(params) {
return db.func('Administracao.SelecionarFeriadoPorId', [
params.idSistema,
params.id
]);
}
That means your query fails, and you are not providing any .catch handler (regular catch in case of ES7 await/async).
See also: http://stackoverflow.com/questions/40500490/what-is-unhandled-promise-rejection
Basically, this is a generic promise-misuse issue, not an issue with this library ;)
I used try catch and still doesn't "work", I traced the exception and it is unhandled because you have a line on your code that is preventing of throwing.
Still, I think you should be more polite when talking to people who uses your library. Or maybe don't answer if you don't want to help.
Thank you.
I used try catch and still doesn't "work", I traced the exception and it is unhandled because you have a line on your code that is preventing of throwing.
You provided none of the details to that end. And I gave you the right answer for the details that you did provide.
I think you should be more polite when talking to people who uses your library. Or maybe don't answer if you don't want to help
Really? I fail to see how. But If you feel that way, then seek help elsewhere.
P.S. Your code looks wrong also, instead of return db.func( there should be return await db.func(.
Hi,
I seem to have the same problem, the following sequence causes it:
const productInstanceCreate = async (req) => {
var instanceId;
var customerId;
try {
//check if we have a customer with this name, if not create one
customerId = (await db.oneOrNone("SELECT customer_id FROM customer WHERE name=$1", [req.params.customer])).customer_id;
log.debug("productInstanceCreate: customerId: %s", JSON.stringify(customerId, null, 1));
return customerId;
} catch (e) {
log.error("productInstanceCreate: error: %s", JSON.stringify(e, null, 1));
throw new Error("productInstanceCreate: error: %s", e);
}
That 'catch' there doesn't see an error thrown, but if I setup a handler for an unhandled rejection I can trap it (but since at that stage it has not context I can't do much with it):
process.on("unhandledRejection", (reason, p) => {
log.error("Unhadled Rejection at: %s, reason: %s", reason, p);
});
I get this (some output omitted):
[2017-06-26T01:49:42.255Z] DEBUG: instance-inventory-api/14349 on backend: productInstanceCreate: customerId: undefined
[2017-06-26T01:49:42.273Z] ERROR: instance-inventory-api/14349 on backend:
Unhadled Rejection at: QueryResultError {
code: queryResultErrorCode.multiple
message: "Multiple rows were not expected."
received: 5
query: "SELECT customer_id FROM customer WHERE name='customer name2'"
}, reason: [object Promise]
@pshemk the code you showed cannot produce that type of problem. I have tried, but cannot reproduce, the rejection is always handled by .catch() correctly.
const test = async () => {
try {
const res = (await db.oneOrNone('select * from users where id > $1', [1])).id;
} catch (error) {
console.log('CAUGHT:', error); // always gets here
}
};
What is your complete environment? -
I'm struggling to reliably replicate this myself. Using a simple snipped like that it works reliably, so it must be my code :-)
node.js: 8.1.2
pg-promise: 6.1.0
os: ubuntu 16.04.2
I'll update this issue if I manage to get a reliable way of replicating the problem.
@pshemk ok, if you can get the steps for this, get back to me. Otherwise, I cannot reproduce it from the example given earlier.
@pshemk Try proper error stack tracing, so that way you at least can post it here, if you ever reproduce the issue:
const promise = require('bluebird');
promise.config({
longStackTraces: true // enable Long-Stack Traces
});
const pgp = require('pg-promise')({
promiseLib: promise
});
Most helpful comment
That means your query fails, and you are not providing any
.catchhandler (regularcatchin case of ES7 await/async).See also: http://stackoverflow.com/questions/40500490/what-is-unhandled-promise-rejection
Basically, this is a generic promise-misuse issue, not an issue with this library ;)