Skip to content

Commit 668f042

Browse files
authored
Merge pull request #443 from machty/mf-throw_on_error_perform_helper
Add onError parameter to perform helper
2 parents 2b8a19a + 139db10 commit 668f042

File tree

2 files changed

+93
-1
lines changed

2 files changed

+93
-1
lines changed

addon/helpers/perform.js

+31-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,38 @@
11
import { helper } from '@ember/component/helper';
2+
import { assert } from '@ember/debug';
23
import { taskHelperClosure } from 'ember-concurrency/-private/helpers';
34

5+
function maybeReportError(onError) {
6+
return function (e) {
7+
if (typeof onError === 'function') {
8+
onError(e);
9+
} else if (onError === null) {
10+
// Do nothing
11+
} else {
12+
assert(
13+
`The onError argument passed to the \`perform\` helper should be a function or null; you passed ${onError}`,
14+
false
15+
);
16+
}
17+
};
18+
}
19+
420
export function performHelper(args, hash) {
5-
return taskHelperClosure('perform', 'perform', args, hash);
21+
let perform = taskHelperClosure('perform', 'perform', args, hash);
22+
23+
if (hash && typeof hash.onError !== 'undefined') {
24+
return function (...innerArgs) {
25+
try {
26+
let taskInstance = perform(...innerArgs);
27+
return taskInstance.catch(maybeReportError(hash.onError));
28+
// eslint-disable-next-line no-empty
29+
} catch {
30+
maybeReportError(hash.onError);
31+
}
32+
};
33+
} else {
34+
return perform;
35+
}
636
}
737

838
export default helper(performHelper);

tests/integration/helpers/perform-test.js

+62
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,66 @@ module('Integration | helpers | perform', function (hooks) {
3535

3636
assert.ok(true);
3737
});
38+
39+
test('can pass onError=null to have it swallow errors thrown from task', async function (assert) {
40+
assert.expect(1);
41+
42+
this.owner.register(
43+
'component:test-swallow-error',
44+
Component.extend({
45+
errorGeneratingTask: task(function* () {
46+
throw new Error('You should not see me!');
47+
}),
48+
})
49+
);
50+
51+
this.owner.register(
52+
'template:components/test-swallow-error',
53+
hbs`
54+
<button {{on 'click' (perform this.errorGeneratingTask onError=null)}}>
55+
I create an error!
56+
</button>
57+
`
58+
);
59+
60+
await render(hbs`<TestSwallowError />`);
61+
62+
await click('button');
63+
64+
assert.ok(true);
65+
});
66+
67+
test('can pass onError=someFn to have it call someFn(e)', async function (assert) {
68+
assert.expect(2);
69+
70+
let error = null;
71+
72+
this.owner.register(
73+
'component:test-swallow-error',
74+
Component.extend({
75+
errorGeneratingTask: task(function* () {
76+
throw new Error('You should not see me!');
77+
}),
78+
errorReport(e) {
79+
error = e;
80+
},
81+
})
82+
);
83+
84+
this.owner.register(
85+
'template:components/test-swallow-error',
86+
hbs`
87+
<button {{on 'click' (perform this.errorGeneratingTask onError=this.errorReport)}}>
88+
I create an error!
89+
</button>
90+
`
91+
);
92+
93+
await render(hbs`<TestSwallowError />`);
94+
95+
await click('button');
96+
97+
assert.ok(true);
98+
assert.ok(error instanceof Error);
99+
});
38100
});

0 commit comments

Comments
 (0)