dumbmatter / fakeIndexedDB

A pure JS in-memory implementation of the IndexedDB API
Apache License 2.0
585 stars 70 forks source link

Throw DOMExceptions, not Errors (maybe?) #48

Closed Quelklef closed 5 months ago

Quelklef commented 4 years ago

fake-indexeddb currently throws exceptions that are Error objects, but IndexedDB throws DOMExceptions.

Of course, this is a little sticky 'cause fake-indexeddb is on Node, but there seems to be a well-known DOMException polyfill that could be used.

Seems like an easy change: just modify src/lib/errors.ts, right?. I can try to make a PR with your go-ahead.

dumbmatter commented 4 years ago

Thanks for the suggestion!

But I don't know if it's worth it... what would be gained, the Error.code property? That's already deprecated and I'm not sure if anyone uses it for IndexedDB. Anything else?

If there's a compelling use case, then yes please go for it. And also in src/test/web-platform-tests/wpt-env.js replace global.DOMException = Error; with importing that module to the global scope.

Quelklef commented 4 years ago

My current use-case is code that looks like the following:

try {
  await doDbOperation();
} catch (err) {
  if (err instanceof DOMException && err.name === 'NotFoundError') {
    throw new MyErrorWrapper('message');
  } else {
    throw err;
  }
}

Cool, I'll see what I can do.

dumbmatter commented 4 years ago

Yeah that makes sense, I'd accept a PR :)