Giter VIP home page Giter VIP logo

Comments (4)

dfahlander avatar dfahlander commented on May 22, 2024

If you catch() the promise that is returned from then(), the error will be picked up as expected:

var db = new Dexie("testdb");
db.version(1).stores({ table1: "id" });
db.on('error', function(err) { console.log(err); });
db.open().then(function () {
    console.log("before");
    throw "FOO";
    console.log("after");
}).catch(function (err) {
   console.log("explicitely catched: " + err);
});

Output:

before
explicitely catched: FOO

However, db.on('error') should pick up all errors that the user code failed to explicitely catch and you have found a case where this fallback event doesnt work as expected. I have already put it in a unit test so that we wont forget to fix it.

from dexie.js.

dfahlander avatar dfahlander commented on May 22, 2024

Having debugged what happens I find that the reason db.on('error') didnt fire, was that the exception wasn't thrown in a database operation nor in a database transaction. That's why it doesnt bubble to db.on('error').

In fact, the code exception occurs when there is no ongoing transaction and the database has been successfully opened and code execution is handed over to the application code. So no db.on('error') should actually be fired.

One could argue that it would be handy with a static event on Promise (Promise.on('uncatched')) but that is another feature.

db.on('error') will only fire in the following scenarios:

  • You are in a transaction scope and an exception occur and you dont catch the Promise returned from the db.transaction() method.
  • You are doing a database operation that fails with an exception and you don't catch the resulting promise.

But not if:

  • You are outside of a transaction and the error doesnt occur from within a database operation.

from dexie.js.

dfahlander avatar dfahlander commented on May 22, 2024

I've updated the documentation for Dexie.on.error to clarify it's behavior.

from dexie.js.

OleLaursen avatar OleLaursen commented on May 22, 2024

Thanks for the quick reply and explanation, I wasn't aware the default behaviour for promises is to swallow exceptions. Gah. I suppose we'll have to append .catch() to all promises then, until something better turns up.

from dexie.js.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.