Skip to content

Commit f8e908a

Browse files
committed
Add onError parameter to perform helper
Allows setting to reporting function for reporting to error handlers, etc. Also allows setting to null to swallow all errors
1 parent a150574 commit f8e908a

File tree

2 files changed

+94
-1
lines changed

2 files changed

+94
-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

+63
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { click, render } from '@ember/test-helpers';
44
import { hbs } from 'ember-cli-htmlbars';
55
import Component from '@ember/component';
66
import { task } from 'ember-concurrency';
7+
import sinon from 'sinon';
78

89
module('Integration | helpers | perform', function (hooks) {
910
setupRenderingTest(hooks);
@@ -35,4 +36,66 @@ module('Integration | helpers | perform', function (hooks) {
3536

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

0 commit comments

Comments
 (0)