[Topic] Catch errors from listeners

This commit is contained in:
Victor Woeltjen
2015-11-09 16:55:22 -08:00
parent acdd9622d2
commit e3e44f74d6
3 changed files with 16 additions and 4 deletions

View File

@@ -207,7 +207,8 @@
}, },
{ {
"key": "topic", "key": "topic",
"implementation": "services/Topic.js" "implementation": "services/Topic.js",
"depends": [ "$log" ]
}, },
{ {
"key": "contextualize", "key": "contextualize",

View File

@@ -26,6 +26,8 @@ define(
function () { function () {
"use strict"; "use strict";
var ERROR_PREFIX = "Error when notifying listener: ";
/** /**
* The `topic` service provides a way to create both named, * The `topic` service provides a way to create both named,
* shared listeners and anonymous, private listeners. * shared listeners and anonymous, private listeners.
@@ -46,7 +48,7 @@ define(
* @returns {Function} * @returns {Function}
* @memberof platform/core * @memberof platform/core
*/ */
function Topic() { function Topic($log) {
var topics = {}; var topics = {};
function createTopic() { function createTopic() {
@@ -63,7 +65,11 @@ define(
}, },
notify: function (message) { notify: function (message) {
listeners.forEach(function (listener) { listeners.forEach(function (listener) {
listener(message); try {
listener(message);
} catch (e) {
$log.error(ERROR_PREFIX + e.message);
}
}); });
} }
}; };

View File

@@ -28,13 +28,18 @@ define(
describe("The 'topic' service", function () { describe("The 'topic' service", function () {
var topic, var topic,
mockLog,
testMessage, testMessage,
mockCallback; mockCallback;
beforeEach(function () { beforeEach(function () {
testMessage = { someKey: "some value"}; testMessage = { someKey: "some value"};
mockLog = jasmine.createSpyObj(
'$log',
[ 'error', 'warn', 'info', 'debug' ]
);
mockCallback = jasmine.createSpy('callback'); mockCallback = jasmine.createSpy('callback');
topic = new Topic(); topic = new Topic(mockLog);
}); });
it("notifies listeners on a topic", function () { it("notifies listeners on a topic", function () {