Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error handling in node-sqlite3

Tags:

node.js

sqlite

How do I handle error in sqlite3? For example, I have this simple code:

var stmt = db.prepare("update Tickets set " + columns + " where id = (?)");
stmt.run(req.body.id);
stmt.finalize();

db.close();

All four functions prepare, run, finalize, close has the potential to throw error. This is on my express.js server so I'm trying to put a res.error() statement somewhere to return result. I don't want to put it in all of them because I can run into a multiple res.setHeader error.

Is there a document on error handling practice with sqlite3? I can't find it in its API documentation.

like image 852
Nam Thai Avatar asked Oct 06 '15 21:10

Nam Thai


2 Answers

Take a look at the api. Each of those functions takes a callback, whose first parameter is an error.

This will help you handle an error, but it will not stop your app from crashing. In order to stop a crash, you'll have to use a try/catch , or preferably learn how to use domains.

like image 105
Academiphile Avatar answered Nov 05 '22 19:11

Academiphile


Errors are emitted on "error" event. you may want to try doing something like below after you initialise your db handle.

db.on("error", function(error) {
    console.log("Getting an error : ", error);
}); 
like image 2
Gaurav S Avatar answered Nov 05 '22 18:11

Gaurav S