Skip to content

Commit

Permalink
Properly re-throw errors from async actions
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcisbee committed Feb 16, 2024
1 parent bde950e commit 989938c
Show file tree
Hide file tree
Showing 7 changed files with 118 additions and 7 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## 2.4.1

### Bugfix
* Properly re-throw errors from async actions.

## 2.4.0

### Feature
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "exome",
"version": "2.4.0",
"version": "2.4.1",
"description": "Proxy based store manager for deeply nested states",
"main": "./dist/exome.js",
"module": "./dist/exome.mjs",
Expand Down
17 changes: 17 additions & 0 deletions src/constructor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,21 @@ test('extended exome instance has "Person" in id', () => {
assert.match(instance[exomeId], /^Person-[A-Z0-9]+$/);
});

test("throws error for async action", async () => {
class TestStore extends Exome {
public async run() {
throw new Error("Poop");
}
}
const test1 = new TestStore();

try {
await test1.run();
assert.unreachable();
} catch (err) {
assert.instance(err, Error);
assert.equal(err.message, "Poop");
}
});

test.run();
4 changes: 3 additions & 1 deletion src/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,9 @@ const { getActionStatus } = proxyquire
`{"loading":true,"error":false}`,
);

await promise;
try {
await promise;
} catch (_) {}

assert.snapshot(
JSON.stringify(test1.status),
Expand Down
85 changes: 85 additions & 0 deletions src/utils/wrapper.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import { suite } from "uvu";
import assert from "uvu/assert";

import { wrapper } from "./wrapper.ts";

{
const test = suite("wrapper");

test("exports `wrapper`", () => {
assert.ok(wrapper);
});

test("that `wrapper` is function", () => {
assert.instance(wrapper, Function);
});

test("doesn't throw with sync success action", () => {
class TestStore {
constructor() {
return wrapper(this as any);
}
public test() {}
}
const testStore = new TestStore();

assert.not.throws(testStore.test);
});

test("doesn't throw with async success action", async () => {
class TestStore {
constructor() {
return wrapper(this as any);
}
public async test() {
await Promise.resolve();
}
}
const testStore = new TestStore();

try {
await testStore.test();
assert.ok(true);
} catch (err) {
assert.instance(err, Error);
assert.equal(err.message, "test error");
}
});

test("throws with sync error action", () => {
class TestStore {
constructor() {
return wrapper(this as any);
}
public test() {
throw new Error("test error");
}
}
const testStore = new TestStore();

assert.throws(testStore.test);
});

test("throws with async error action", async () => {
class TestStore {
constructor() {
return wrapper(this as any);
}
public async test() {
await Promise.resolve();
throw new Error("test error");
}
}
const testStore = new TestStore();

try {
await testStore.test();
assert.unreachable();
} catch (err) {
assert.instance(err, Error);
assert.equal(err.message, "test error");
}
});

test.run();
}
8 changes: 5 additions & 3 deletions src/utils/wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@ export const wrapper = <T extends Exome>(parent: T): T => {
const output = value.apply(parent, args);

if (output instanceof Promise) {
return output
.then((result) => (middleware(), result))
.catch(middleware);
return new Promise<any>((resolve, reject) => {
output
.then((result) => (middleware(), resolve(result)))
.catch((error) => (reject(error), middleware(error)));
});
}

return middleware(), output;
Expand Down

0 comments on commit 989938c

Please sign in to comment.