-
-
Notifications
You must be signed in to change notification settings - Fork 157
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
Showing
2 changed files
with
94 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters