Skip to content

Commit f5eff82

Browse files
committed
fix: unhandledRejection error, issue #996
1 parent ea0d0c7 commit f5eff82

File tree

3 files changed

+36
-12
lines changed

3 files changed

+36
-12
lines changed

lib/odbc.js

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -661,24 +661,35 @@ Database.prototype.query = function (query, params, cb)
661661
sql = query;
662662
}
663663
exports.debug && console.log(getElapsedTime(), "odbc.js:query() => ", sql);
664-
if (params && params.length > 0)
665-
{
666-
if(Array.isArray(params))
664+
665+
try {
666+
if (params && params.length > 0)
667667
{
668-
var err = parseParams(params);
669-
if(err) deferred ? deferred.reject(err) : cb(err);
668+
if(Array.isArray(params))
669+
{
670+
var err = parseParams(params);
671+
if(err) deferred ? deferred.reject(err) : cb(err);
672+
}
673+
if(exports.debug && logParams) {
674+
console.log("%s odbc.js:query() params = ", getElapsedTime(), params);
675+
}
676+
if(typeof query === 'object')
677+
{
678+
query.params = params;
679+
self.conn.query(query, cbQuery);
680+
}
681+
else
682+
self.conn.query(query, params, cbQuery);
670683
}
671-
if(typeof query === 'object')
684+
else
672685
{
673-
query.params = params;
674686
self.conn.query(query, cbQuery);
675687
}
676-
else
677-
self.conn.query(query, params, cbQuery);
678-
}
679-
else
688+
} catch (err)
680689
{
681-
self.conn.query(query, cbQuery);
690+
self.checkConnectionError(err);
691+
deferred ? deferred.reject(err) : cb(err);
692+
return next();
682693
}
683694
}); //self.queue.push
684695
return deferred ? deferred.promise : false;

src/odbc_connection.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1064,6 +1064,8 @@ NAN_METHOD(ODBCConnection::Query)
10641064
GETCPPSTR2(data->sql, sql3, len, errmsg);
10651065
}
10661066

1067+
Nan::TryCatch try_catch;
1068+
10671069
Local<String> optionParamsKey = Nan::New(OPTION_PARAMS);
10681070
if (Nan::HasOwnProperty(obj, optionParamsKey).IsJust() && Nan::Get(obj, optionParamsKey).ToLocalChecked()->IsArray())
10691071
{
@@ -1076,6 +1078,13 @@ NAN_METHOD(ODBCConnection::Query)
10761078
data->paramCount = 0;
10771079
}
10781080

1081+
if (try_catch.HasCaught())
1082+
{
1083+
free(data);
1084+
try_catch.ReThrow();
1085+
return;
1086+
}
1087+
10791088
Local<String> optionNoResultsKey = Nan::New(OPTION_NORESULTS);
10801089
if (Nan::HasOwnProperty(obj, optionNoResultsKey).IsJust() && Nan::Get(obj, optionNoResultsKey).ToLocalChecked()->IsBoolean())
10811090
{

test/test-asyc-await.js renamed to test/test-async-await.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ var common = require("./common")
55
, cn = common.connectionString
66
;
77

8+
process.on('unhandledRejection', async (reason, p) => {
9+
console.debug('Unhandled Error: ', reason, p)
10+
});
11+
812
main();
913
async function main() {
1014
await test1();

0 commit comments

Comments
 (0)