Picture of Rico 1837
Registered 10 years 302 days
Rico 1837 Wednesday, 16 June 2021, 12:57 PM
[JS SDK] return a Error on failed database actions
I have a field in one of my database with a unique key.

Database.insert("INSERT INTO users (sip, domain) VALUES ('alice','example.com')")
.oncomplete(function(id) { log("created id=" + id); })
.onerror(function() {
log("some kind of database error happend, dont know details");
});

When a users tries to add a non-unique entry, the Database.onerror() is called.
But without the error.

At the stdout of the appservice i can see the error message and something that looks like the distinct error code 23505

PostgreSQLDatabase(00185d58,0017c1b8)::LastCommandFailed (23505):ERROR: duplicate key value violates unique constraint "domain" DETAIL: Key (domain)=(example.com) already exists.

I would like to receive this errortext and errorid to send the user a more detailed error-message with the actual reason than "error happend".

So i would like to have the error message and/or text to be includes as parameter.

Database.insert("INSERT INTO users (sip, domain) VALUES ('alice','example.com')")
.oncomplete(function(id) { log("created id=" + id); })
.onerror(function(errorid, errortext) {
if(errorid == 23505) {
log("sip or domain exists already");
} else {
log("unhandled db error: " + errortext);
}
});
Picture of Matthias Schertler (innovaphone)
Moderator Registered 12 years 192 days
Matthias Schertler (innovaphone) Wednesday, 16 June 2021, 11:27 PM
Re: [JS SDK] return a Error on failed database actions
Hi Rico,

thanks for your feedback. At a first glance it looks like we can add at least the errorText to the callback. We will investigate and come back to you.

In our own apps we solved similar problems in the SQL query itself, by using a pattern like

INSERT INTO ... VALUES ... ON CONFLICT DO UPDATE ...

Maybe this is an option in your application too?

BR
Matthias

Picture of Matthias Schertler (innovaphone)
Moderator Registered 12 years 192 days
Matthias Schertler (innovaphone) Thursday, 17 June 2021, 05:30 PM
Re: [JS SDK] return a Error on failed database actions
Hi Rico,

for the next SDK build we changed the interface of the onerror callback according to your request.

Actually we added three parameters:
  • error
  • errorText
  • dbErrorCode - this would be the one you are looking for
Please see the updated documentation for details:
https://sdk.innovaphone.com/13r2/doc/javascript/Database.htm#DatabaseOperation_onerror

Note: you need to wait for the next beta build to try it.

BR and thanks again!
Matthias
← You can define your color theme preference here