Skip to content

Commit

Permalink
Use DOMException rather than Error, since all supported versions of N…
Browse files Browse the repository at this point in the history
…ode.js have that built-in now and that follows the IndexedDB spec better - closes #48
  • Loading branch information
dumbmatter committed May 20, 2024
1 parent f0c1e0f commit 740284a
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 52 deletions.
6 changes: 2 additions & 4 deletions src/FDBFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,8 +235,7 @@ class FDBFactory {

deleteDatabase(this._databases, name, request, (err) => {
if (err) {
request.error = new Error();
request.error.name = err.name;
request.error = new DOMException(err.message, err.name);
request.readyState = "done";

const event = new FakeEvent("error", {
Expand Down Expand Up @@ -288,8 +287,7 @@ class FDBFactory {
request.result = undefined;
request.readyState = "done";

request.error = new Error();
request.error.name = err.name;
request.error = new DOMException(err.message, err.name);

const event = new FakeEvent("error", {
bubbles: true,
Expand Down
3 changes: 1 addition & 2 deletions src/FDBTransaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,7 @@ class FDBTransaction extends FakeEventTarget {
}

if (errName !== null) {
const e = new Error();
e.name = errName;
const e = new DOMException(undefined, errName);
this.error = e;
}

Expand Down
60 changes: 20 additions & 40 deletions src/lib/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,82 +20,62 @@ const messages = {
"An attempt was made to open a database using a lower version than the existing version.",
};

export class AbortError extends Error {
export class AbortError extends DOMException {
constructor(message = messages.AbortError) {
super();
this.name = "AbortError";
this.message = message;
super(message, "AbortError");
}
}

export class ConstraintError extends Error {
export class ConstraintError extends DOMException {
constructor(message = messages.ConstraintError) {
super();
this.name = "ConstraintError";
this.message = message;
super(message, "ConstraintError");
}
}

export class DataCloneError extends Error {
export class DataCloneError extends DOMException {
constructor(message = messages.DataCloneError) {
super();
this.name = "DataCloneError";
this.message = message;
super(message, "DataCloneError");
}
}

export class DataError extends Error {
export class DataError extends DOMException {
constructor(message = messages.DataError) {
super();
this.name = "DataError";
this.message = message;
super(message, "DataError");
}
}

export class InvalidAccessError extends Error {
export class InvalidAccessError extends DOMException {
constructor(message = messages.InvalidAccessError) {
super();
this.name = "InvalidAccessError";
this.message = message;
super(message, "InvalidAccessError");
}
}

export class InvalidStateError extends Error {
export class InvalidStateError extends DOMException {
constructor(message = messages.InvalidStateError) {
super();
this.name = "InvalidStateError";
this.message = message;
super(message, "InvalidStateError");
}
}

export class NotFoundError extends Error {
export class NotFoundError extends DOMException {
constructor(message = messages.NotFoundError) {
super();
this.name = "NotFoundError";
this.message = message;
super(message, "NotFoundError");
}
}

export class ReadOnlyError extends Error {
export class ReadOnlyError extends DOMException {
constructor(message = messages.ReadOnlyError) {
super();
this.name = "ReadOnlyError";
this.message = message;
super(message, "ReadOnlyError");
}
}

export class TransactionInactiveError extends Error {
export class TransactionInactiveError extends DOMException {
constructor(message = messages.TransactionInactiveError) {
super();
this.name = "TransactionInactiveError";
this.message = message;
super(message, "TransactionInactiveError");
}
}

export class VersionError extends Error {
export class VersionError extends DOMException {
constructor(message = messages.VersionError) {
super();
this.name = "VersionError";
this.message = message;
super(message, "VersionError");
}
}
11 changes: 5 additions & 6 deletions src/test/web-platform-tests/wpt-env.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ global.document = {
// this will instead use another object that also can't be used as a key.
getElementsByTagName: () => Math,
};
global.DOMException = Error; // Kind of cheating for error-attributes.js
global.location = {
location: {},
};
Expand Down Expand Up @@ -50,7 +49,7 @@ const assert_class_string = (object, class_string, description) => {
assert_equals(
object.toString(),
"[object " + class_string + "]",
description
description,
);
};

Expand Down Expand Up @@ -143,7 +142,7 @@ class AsyncTest {
this.step_func(() => {
return fn.apply(this, args);
}),
timeout
timeout,
);
}

Expand Down Expand Up @@ -199,7 +198,7 @@ function EventWatcher(test, watchedNode, eventTypes) {
var eventHandler = test.step_func(function (evt) {
assert_true(
!!waitingFor,
"Not expecting event, but got " + evt.type + " event"
"Not expecting event, but got " + evt.type + " event",
);
assert_equals(
evt.type,
Expand All @@ -208,7 +207,7 @@ function EventWatcher(test, watchedNode, eventTypes) {
waitingFor.types[0] +
" event, but got " +
evt.type +
" event instead"
" event instead",
);
if (waitingFor.types.length > 1) {
// Pop first event from array
Expand Down Expand Up @@ -431,7 +430,7 @@ const promise_test = (func, name, properties) => {
.catch(
test.step_func(function (value) {
throw value;
})
}),
);
return donePromise;
});
Expand Down

0 comments on commit 740284a

Please sign in to comment.