Using Phonegap you can set a function to be called back if the whole database transaction or the individual SQL statement errors. I'd like to know how to get more information about the error.
I have one generic error-handling function, and lots of different SELECTs or INSERTs that may trigger it. How can I tell which one was at fault? It is not always obvious from the error message.
My code so far is...
function get_rows(tx) {
tx.executeSql("SELECT * FROM Blah", [], lovely_success, statement_error);
}
function add_row(tx) {
tx.executeSql("INSERT INTO Blah (1, 2, 3)", [], carry_on, statement_error);
}
function statement_error(tx, error) {
alert(error.code + ' / ' + error.message);
}
From various examples I see the error callback will be passed a transaction
object and an error
object. I read that .code
can have the following values:
Are there any other properties/methods of the error object?
What are the properties/methods of the transaction object at this point?
I can't seem to find a good online reference for this. Certainly not on the Phonegap website!
http://docs.phonegap.com/en/2.2.0/cordova_storage_storage.md.html#SQLError
The SQLError object is thrown when an error occurs when manipulating a database.
This object has two properties:
So in your code error.message
will give you a nice description of what went wrong in which transaction.
I think that's exactly what you want, isn't it?
Best regards, F481
The only thing you can do with the transaction object is call its .executeSql()
method, as far as I can ascertain. I cannot find any properties of this object.
The error object has a .code
property which contains a number. You can either check the numerical value (see my original question above) or use something like:if (error.code == error.DATABASE_ERR) alert('nasty database error')
The .message
property is a string and may return something like this:
Other messages are possible! This is just the few I spotted when debugging in Chrome. I notice in Phonegap the messages are briefer: "no such table: MyyTable"
Also note that there is another database error callback on the initial call to .transaction()
. Your function will only be returned an error object (no transaction object).
The error's .code
will be zero and the .message
will be "the statement callback raised an exception or statement error callback did not return false".
So remember to have your statement callbacks (function mentioned inside .executeSql
such as my statement_error in the code example of my original question) return true or false depending on whether you want your transaction error callback (second function mentioned inside .transaction
) to be hit. The 'success' callback you specified (third one inside .transaction
) will be run if you return true (or don't return anything).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With