mirror of
https://github.com/nasa/openmct.git
synced 2025-02-21 09:52:04 +00:00
[Action] Catch errors
Catch errors during action instantiation, to avoid suppressing whole context menus on single failures; nasa/openmctweb#120.
This commit is contained in:
parent
96a7c12d69
commit
07179f7290
@ -92,7 +92,7 @@
|
||||
"provides": "actionService",
|
||||
"type": "provider",
|
||||
"implementation": "actions/ActionProvider.js",
|
||||
"depends": [ "actions[]" ]
|
||||
"depends": [ "actions[]", "$log" ]
|
||||
},
|
||||
{
|
||||
"provides": "actionService",
|
||||
|
@ -39,9 +39,11 @@ define(
|
||||
* @imeplements {ActionService}
|
||||
* @constructor
|
||||
*/
|
||||
function ActionProvider(actions) {
|
||||
function ActionProvider(actions, $log) {
|
||||
var self = this;
|
||||
|
||||
this.$log = $log;
|
||||
|
||||
// Build up look-up tables
|
||||
this.actions = actions;
|
||||
this.actionsByKey = {};
|
||||
@ -74,6 +76,7 @@ define(
|
||||
var context = (actionContext || {}),
|
||||
category = context.category,
|
||||
key = context.key,
|
||||
$log = this.$log,
|
||||
candidates;
|
||||
|
||||
// Instantiate an action; invokes the constructor and
|
||||
@ -103,12 +106,31 @@ define(
|
||||
// appliesTo method of given actions (if defined), and
|
||||
// instantiate those applicable actions.
|
||||
function createIfApplicable(actions, context) {
|
||||
return (actions || []).filter(function (Action) {
|
||||
return Action.appliesTo ?
|
||||
Action.appliesTo(context) : true;
|
||||
}).map(function (Action) {
|
||||
return instantiateAction(Action, context);
|
||||
});
|
||||
function isApplicable(Action) {
|
||||
return Action.appliesTo ? Action.appliesTo(context) : true;
|
||||
}
|
||||
|
||||
function instantiate(Action) {
|
||||
try {
|
||||
return instantiateAction(Action, context);
|
||||
} catch (e) {
|
||||
$log.error([
|
||||
"Could not instantiate action",
|
||||
Action.key,
|
||||
"due to:",
|
||||
e.message
|
||||
].join(" "));
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
|
||||
function isDefined(action) {
|
||||
return action !== undefined;
|
||||
}
|
||||
|
||||
return (actions || []).filter(isApplicable)
|
||||
.map(instantiate)
|
||||
.filter(isDefined);
|
||||
}
|
||||
|
||||
// Match actions to the provided context by comparing "key"
|
||||
|
Loading…
x
Reference in New Issue
Block a user