Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the context of a Web Sql error?

I start deploying an offline application on iPhones, but it's still under heavy developpment. I have a simple error handler for my query :

db.transaction(tx) {
    tx.executeSql("SELECT * FROM TABLE",[], successHandler, errorHandler);
});
function errorHandler(transaction, error) {
    alert("Error : " + error.message);
}

When I test myself the application, and get an error, I manage to find what was the query generating the error. But when it's my users (distant users, of course), it's very difficult, as the error messages are not specific.

Is there a way to add context info to my error messages, for example the sql query, or a comment parameter ?

like image 913
Matthieu Avatar asked May 15 '13 08:05

Matthieu


2 Answers

You could use a pattern like this:

  db.transaction(tx) {
    doQuery(tx, "SELECT * FROM TABLE",[],theSuccessHandler)
  });

  function doQuery(tx, query, values, successHandler) {
    tx.executeSql(query, values, successHandler, errorHandler);
    function errorHandler(transaction, error) {
        alert("Error : " + error.message + " in " + query);
    }
  }
like image 72
Myrne Stol Avatar answered Sep 28 '22 08:09

Myrne Stol


I ehanced Myrne answer to add query parameters and a free context string :

function doQuery(tx, query, values, successHandler, context)
{
    tx.executeSql(query, values, successHandler, errorHandler);
    function errorHandler(transaction, error)
    {
        var text_context = context != undefined && context != "" ? "(" + context + ") " : "";
        alert("Error "+text_context+": " + error.message + " in " + query + " (params : "+values.join(", ")+")");
    }
}

This will return this kind of error :

Error (function update_commande) : could not prepare statement (1 no such column: field3) in UPDATE table SET field2 = ?, field3 = ? WHERE field1 = ? (params : 1.63, 1449, 606)

like image 29
Matthieu Avatar answered Sep 28 '22 08:09

Matthieu