mirror of
https://github.com/nasa/openmct.git
synced 2025-02-20 17:33:23 +00:00
Merge pull request #273 from nasa/open231
[Core] Catch errors from listeners in topic
This commit is contained in:
commit
f9a7ca85ac
@ -217,7 +217,8 @@
|
||||
},
|
||||
{
|
||||
"key": "topic",
|
||||
"implementation": "services/Topic.js"
|
||||
"implementation": "services/Topic.js",
|
||||
"depends": [ "$log" ]
|
||||
},
|
||||
{
|
||||
"key": "contextualize",
|
||||
|
@ -26,6 +26,8 @@ define(
|
||||
function () {
|
||||
"use strict";
|
||||
|
||||
var ERROR_PREFIX = "Error when notifying listener: ";
|
||||
|
||||
/**
|
||||
* The `topic` service provides a way to create both named,
|
||||
* shared listeners and anonymous, private listeners.
|
||||
@ -46,7 +48,7 @@ define(
|
||||
* @returns {Function}
|
||||
* @memberof platform/core
|
||||
*/
|
||||
function Topic() {
|
||||
function Topic($log) {
|
||||
var topics = {};
|
||||
|
||||
function createTopic() {
|
||||
@ -63,7 +65,11 @@ define(
|
||||
},
|
||||
notify: function (message) {
|
||||
listeners.forEach(function (listener) {
|
||||
listener(message);
|
||||
try {
|
||||
listener(message);
|
||||
} catch (e) {
|
||||
$log.error(ERROR_PREFIX + e.message);
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
|
@ -28,13 +28,18 @@ define(
|
||||
|
||||
describe("The 'topic' service", function () {
|
||||
var topic,
|
||||
mockLog,
|
||||
testMessage,
|
||||
mockCallback;
|
||||
|
||||
beforeEach(function () {
|
||||
testMessage = { someKey: "some value"};
|
||||
mockLog = jasmine.createSpyObj(
|
||||
'$log',
|
||||
[ 'error', 'warn', 'info', 'debug' ]
|
||||
);
|
||||
mockCallback = jasmine.createSpy('callback');
|
||||
topic = new Topic();
|
||||
topic = new Topic(mockLog);
|
||||
});
|
||||
|
||||
it("notifies listeners on a topic", function () {
|
||||
@ -65,6 +70,21 @@ define(
|
||||
expect(mockCallback).toHaveBeenCalledWith(testMessage);
|
||||
});
|
||||
|
||||
it("is robust against errors thrown by listeners", function () {
|
||||
var mockBadCallback = jasmine.createSpy("bad-callback"),
|
||||
t = topic();
|
||||
|
||||
mockBadCallback.andCallFake(function () {
|
||||
throw new Error("I'm afraid I can't do that.");
|
||||
});
|
||||
|
||||
t.listen(mockBadCallback);
|
||||
t.listen(mockCallback);
|
||||
|
||||
t.notify(testMessage);
|
||||
expect(mockCallback).toHaveBeenCalledWith(testMessage);
|
||||
});
|
||||
|
||||
});
|
||||
}
|
||||
);
|
||||
|
Loading…
x
Reference in New Issue
Block a user