Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Verbose doesn't work with pormises #1316

Closed
mmghv opened this issue Apr 27, 2020 · 0 comments · Fixed by #1317
Closed

Verbose doesn't work with pormises #1316

mmghv opened this issue Apr 27, 2020 · 0 comments · Fixed by #1317

Comments

@mmghv
Copy link
Contributor

mmghv commented Apr 27, 2020

If I try to use Promises with node-sqlite3, verbose() doesn't work.

consider the following example:

const sqlite3 = require('sqlite3').verbose();

const db = new sqlite3.Database(':memory:');

function db_run(sql){
  return new Promise((resolve, reject) => {
    db.run(sql, err => {
      if (err) {
        console.log('callback has error ...');
        reject(err);
      } else {
        resolve()
      }
    });
  });
}

db_run('update non_existent_table set id=1').catch(err => {
  console.log('promise rejected ...');
  throw err;
});

This works as expected and I get the promise rejected, but the error I get doesn't have any trace information :

$ node app
callback has error ...
promise rejected ...
(node:15600) UnhandledPromiseRejectionWarning: Error: SQLITE_ERROR: no such table: non_existent_table
(node:15600) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:15600) [DEP0018] 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.

The docs says that I should throw the error, if I throw it in Promise.catch() I don't get the trace,
But if I throw it in db_run in the callback instead of reject(err), I get the trace information but the promise never rejects because throw err happens in an async callback so the promise doesn't catch it, neither does placing a try/catch around the callback.

$ node app
callback has error ...
C:\sqlite-test\node_modules\sqlite3\lib\trace.js:36
                    throw err;
                    ^

Error: SQLITE_ERROR: no such table: non_existent_table
--> in Database#run('update non_existent_table set id=1', [Function])
    at C:\sqlite-test\app.js:6:8
    at new Promise (<anonymous>)
    at db_run (C:\sqlite-test\app.js:5:10)
    at Object.<anonymous> (C:\sqlite-test\app.js:18:1)
←[90m    at Module._compile (internal/modules/cjs/loader.js:955:30)←[39m
←[90m    at Object.Module._extensions..js (internal/modules/cjs/loader.js:991:10)←[39m
←[90m    at Module.load (internal/modules/cjs/loader.js:811:32)←[39m
←[90m    at Function.Module._load (internal/modules/cjs/loader.js:723:14)←[39m
←[90m    at Function.Module.runMain (internal/modules/cjs/loader.js:1043:10)←[39m {
  errno: ←[33m1←[39m,
  code: ←[32m'SQLITE_ERROR'←[39m,
  __augmented: ←[33mtrue←[39m

p.s. working on a PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant