How to show useful error messages from a database error callback in Phonegap?

With Phonegap, you can set up a function that will be called back if the entire database transaction or individual SQL query errors. I would like to know how to get more information about the error.

I have one common error handling function and many different SELECT or INSERT that can call it. How can I find out which one was to blame? This is not always obvious from the error message.

My code is still ...

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 that the error callback will be passed to the transaction object and the error object. I read that .code can have the following meanings:

  • UNKNOWN_ERR = 0
  • DATABASE_ERR = 1
  • VERSION_ERR = 2
  • TOO_LARGE_ERR = 3
  • QUOTA_ERR = 4
  • SYNTAX_ERR = 5
  • CONSTRAINT_ERR = 6
  • TIMEOUT_ERR = 7

Are there any other properties / methods of the error object?
What are the properties / methods of the transaction object at this point?

I can not find a good online link for this. Of course not on Phonegap!

+7
source share
2 answers

Transaction object

The only thing you can do with the transaction object is the .executeSql() method, as far as I can tell. I can not find any properties of this object.

Error object

The error object has a .code property that contains a number. You can either check the numerical value (see My initial 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:

  • failed to prepare statement (1 near "wibble": syntax error)
  • failed to prepare statement (1 there is no such table: MyyTable)
  • failed to prepare statement (1 table MyTable does not have a column MyColunm)
  • failed to execute instruction (failure 19 restrictions)

Other posts are possible! These are just the few that I noticed when debugging in Chrome. I notice that in Phonegap the messages are shorter: "there is no such table: MyyTable"

There are two sets of success / error callbacks

Also note that the first call to .transaction() has another database error callback. Only an error object will be returned to your function (without a transaction object).

The .code error will be zero, and the .message will be a "statement callback caused by a callback with an exception or expression error, does not return false."

So remember that you are calling callbacks (the function specified inside .executeSql , for example my statement_error in the sample code of my source question) returns true or false depending on whether you want a transaction error callback (the second function is mentioned inside .transaction ) The success callback that you specified (the third inside .transaction ) will be triggered if you return true (or return nothing).

+3
source

http://docs.phonegap.com/en/2.2.0/cordova_storage_storage.md.html#SQLError

The SQLError object is raised when an error occurs while working with the database.

This object has two properties:

  • (one of your constants)
  • message: error description.

So, in your error.message code error.message you get a good description of what went wrong in which transaction.

I think that is exactly what you want, right?

Regards, F481

+11
source

All Articles