最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to get the context of a Web Sql error? - Stack Overflow

programmeradmin2浏览0评论

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 ment parameter ?

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 ment parameter ?

Share Improve this question asked May 15, 2013 at 8:06 MatthieuMatthieu 5631 gold badge8 silver badges26 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 11

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);
    }
  }

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_mande) : could not prepare statement (1 no such column: field3) in UPDATE table SET field2 = ?, field3 = ? WHERE field1 = ? (params : 1.63, 1449, 606)

发布评论

评论列表(0)

  1. 暂无评论