Sitelet https://github.com/Marcisbee/exome/commit/989938c9c0616565de5a2a31218e2f7ab0a4153a
Skip to content

Commit 989938c

Browse files
committed
Properly re-throw errors from async actions
1 parent bde950e commit 989938c

7 files changed

Lines changed: 118 additions & 7 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Changelog
22

3+
## 2.4.1
4+
5+
### Bugfix
6+
* Properly re-throw errors from async actions.
7+
38
## 2.4.0
49

510
### Feature

‎package-lock.json‎

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "exome",
3-
"version": "2.4.0",
3+
"version": "2.4.1",
44
"description": "Proxy based store manager for deeply nested states",
55
"main": "./dist/exome.js",
66
"module": "./dist/exome.mjs",

‎src/constructor.test.ts‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,21 @@ test('extended exome instance has "Person" in id', () => {
4141
assert.match(instance[exomeId], /^Person-[A-Z0-9]+$/);
4242
});
4343

44+
test("throws error for async action", async () => {
45+
class TestStore extends Exome {
46+
public async run() {
47+
throw new Error("Poop");
48+
}
49+
}
50+
const test1 = new TestStore();
51+
52+
try {
53+
await test1.run();
54+
assert.unreachable();
55+
} catch (err) {
56+
assert.instance(err, Error);
57+
assert.equal(err.message, "Poop");
58+
}
59+
});
60+
4461
test.run();

‎src/utils.test.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,9 @@ const { getActionStatus } = proxyquire
124124
`{"loading":true,"error":false}`,
125125
);
126126

127-
await promise;
127+
try {
128+
await promise;
129+
} catch (_) {}
128130

129131
assert.snapshot(
130132
JSON.stringify(test1.status),

‎src/utils/wrapper.test.ts‎

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import { suite } from "uvu";
2+
import assert from "uvu/assert";
3+
4+
import { wrapper } from "./wrapper.ts";
5+
6+
{
7+
const test = suite("wrapper");
8+
9+
test("exports `wrapper`", () => {
10+
assert.ok(wrapper);
11+
});
12+
13+
test("that `wrapper` is function", () => {
14+
assert.instance(wrapper, Function);
15+
});
16+
17+
test("doesn't throw with sync success action", () => {
18+
class TestStore {
19+
constructor() {
20+
return wrapper(this as any);
21+
}
22+
public test() {}
23+
}
24+
const testStore = new TestStore();
25+
26+
assert.not.throws(testStore.test);
27+
});
28+
29+
test("doesn't throw with async success action", async () => {
30+
class TestStore {
31+
constructor() {
32+
return wrapper(this as any);
33+
}
34+
public async test() {
35+
await Promise.resolve();
36+
}
37+
}
38+
const testStore = new TestStore();
39+
40+
try {
41+
await testStore.test();
42+
assert.ok(true);
43+
} catch (err) {
44+
assert.instance(err, Error);
45+
assert.equal(err.message, "test error");
46+
}
47+
});
48+
49+
test("throws with sync error action", () => {
50+
class TestStore {
51+
constructor() {
52+
return wrapper(this as any);
53+
}
54+
public test() {
55+
throw new Error("test error");
56+
}
57+
}
58+
const testStore = new TestStore();
59+
60+
assert.throws(testStore.test);
61+
});
62+
63+
test("throws with async error action", async () => {
64+
class TestStore {
65+
constructor() {
66+
return wrapper(this as any);
67+
}
68+
public async test() {
69+
await Promise.resolve();
70+
throw new Error("test error");
71+
}
72+
}
73+
const testStore = new TestStore();
74+
75+
try {
76+
await testStore.test();
77+
assert.unreachable();
78+
} catch (err) {
79+
assert.instance(err, Error);
80+
assert.equal(err.message, "test error");
81+
}
82+
});
83+
84+
test.run();
85+
}

‎src/utils/wrapper.ts‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,11 @@ export const wrapper = <T extends Exome>(parent: T): T => {
2525
const output = value.apply(parent, args);
2626

2727
if (output instanceof Promise) {
28-
return output
29-
.then((result) => (middleware(), result))
30-
.catch(middleware);
28+
return new Promise<any>((resolve, reject) => {
29+
output
30+
.then((result) => (middleware(), resolve(result)))
31+
.catch((error) => (reject(error), middleware(error)));
32+
});
3133
}
3234

3335
return middleware(), output;

0 commit comments

Comments
 (0)