Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add onError parameter to perform helper #443

Merged
merged 2 commits into from
Oct 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
62 changes: 62 additions & 0 deletions tests/integration/helpers/perform-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,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);
});
});