5

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 ?

Matthieu
  • 563
  • 1
  • 7
  • 25

2 Answers2

12

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);
    }
  }
Myrne Stol
  • 11,222
  • 4
  • 40
  • 48
  • So how would you do this if the "function errorHandler(..." was NOT inside the doQuery function? I have many tx.executeSql statements in my code, and I don't want to make them all into your doQuery example above. I need to use an external error callback function. Can I somehow pass parameters into my sqlErrorHandler function, other than the tx and error parameters it automatically passes? – Laurence MacNeill Jan 12 '23 at 18:06
1

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)

Matthieu
  • 563
  • 1
  • 7
  • 25