Skip to content

Commit

Permalink
Add onError parameter to perform helper
Browse files Browse the repository at this point in the history
Allows setting to reporting function for reporting to error handlers, etc.
Also allows setting to null to swallow all errors
  • Loading branch information
maxfierke committed Sep 19, 2021
1 parent a150574 commit f8e908a
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 1 deletion.
32 changes: 31 additions & 1 deletion addon/helpers/perform.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,38 @@
import { helper } from '@ember/component/helper';
import { assert } from '@ember/debug';
import { taskHelperClosure } from 'ember-concurrency/-private/helpers';

function maybeReportError(onError) {
return function (e) {
if (typeof onError === 'function') {
onError(e);
} else if (onError === null) {
// Do nothing
} else {
assert(
`The onError argument passed to the \`perform\` helper should be a function or null; you passed ${onError}`,
false
);
}
};
}

export function performHelper(args, hash) {
return taskHelperClosure('perform', 'perform', args, hash);
let perform = taskHelperClosure('perform', 'perform', args, hash);

if (hash && typeof hash.onError !== 'undefined') {
return function (...innerArgs) {
try {
let taskInstance = perform(...innerArgs);
return taskInstance.catch(maybeReportError(hash.onError));
// eslint-disable-next-line no-empty
} catch {
maybeReportError(hash.onError);
}
};
} else {
return perform;
}
}

export default helper(performHelper);
63 changes: 63 additions & 0 deletions tests/integration/helpers/perform-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { click, render } from '@ember/test-helpers';
import { hbs } from 'ember-cli-htmlbars';
import Component from '@ember/component';
import { task } from 'ember-concurrency';
import sinon from 'sinon';

module('Integration | helpers | perform', function (hooks) {
setupRenderingTest(hooks);
Expand Down Expand Up @@ -35,4 +36,66 @@ module('Integration | helpers | perform', function (hooks) {

assert.ok(true);
});

test('can pass onError=null to have it swallow errors thrown from task', async function (assert) {
assert.expect(1);

this.owner.register(
'component:test-swallow-error',
Component.extend({
errorGeneratingTask: task(function* () {
throw new Error('You should not see me!');
}),
})
);

this.owner.register(
'template:components/test-swallow-error',
hbs`
<button {{on 'click' (perform this.errorGeneratingTask onError=null)}}>
I create an error!
</button>
`
);

await render(hbs`<TestSwallowError />`);

await click('button');

assert.ok(true);
});

test('can pass onError=someFn to have it call someFn(e)', async function (assert) {
assert.expect(2);

let error = null;

this.owner.register(
'component:test-swallow-error',
Component.extend({
errorGeneratingTask: task(function* () {
throw new Error('You should not see me!');
}),
errorReport(e) {
error = e;
},
})
);

this.owner.register(
'template:components/test-swallow-error',
hbs`
<button {{on 'click' (perform this.errorGeneratingTask onError=this.errorReport)}}>
I create an error!
</button>
`
);

await render(hbs`<TestSwallowError />`);

await click('button');

assert.ok(true);
assert.ok(error instanceof Error);
});
});

0 comments on commit f8e908a

Please sign in to comment.