Permalink
Browse files

Improved error tolerance in LocalLink and test interface.

  • Loading branch information...
1 parent 471f1f4 commit c4dd5f32afc79b3a231ee3cf6df9a043be4afa19 @devyn devyn committed Mar 28, 2012
Showing with 24 additions and 5 deletions.
  1. +8 −1 hub/Hub.js
  2. +16 −4 hub/LocalLink.js
View
@@ -39,7 +39,14 @@ Hub.prototype.init = function () {
} else if (md = line.match (/^\/to (.*)/)) {
to = md[1].trim ();
} else if (md = line.match (/^\/(?:([^ :]+)\:)?([^ ]+)(?: (.*)|$)/)) {
- if (to) self.command_manager.dispatch (md[1] || to, md[2].trim(), JSON.parse (md[3]||"null"));
+ var data;
+
+ try {
+ data = JSON.parse (md[3]||"null");
+ } catch (e) {
+ data = md[3];
+ }
+ if (to) self.command_manager.dispatch (md[1] || to, md[2].trim(), data);
} else {
if (to) self.command_manager.dispatch (to, "message", line.trim());
}
View
@@ -33,7 +33,15 @@ LocalLink.prototype.item = function (id) {
};
LocalLink.prototype.respond = function (handler) {
- this.hub.command_manager.define_provider (this.petal_name, handler);
+ var wrapped_handler = function (item, command, data, success, error) {
+ try {
+ handler (item, command, data, success, error);
+ } catch (e) {
+ error ({type: "NativeException", description: "" + e});
+ }
+ };
+
+ this.hub.command_manager.define_provider (this.petal_name, wrapped_handler);
};
var Item = function (hub, petal_name, id) {
@@ -46,9 +54,13 @@ Item.prototype.subscribe = function (type, handler) {
var self = this;
var wrapped_handler = function (item, type, data) {
- handler ({item: item, type: type, data: data, next: function () {
- self.hub.event_manager.next (self.petal_name, this.item, this.type, this.data);
- }});
+ try {
+ handler ({item: item, type: type, data: data, next: function () {
+ self.hub.event_manager.next (self.petal_name, this.item, this.type, this.data);
+ }});
+ } catch (e) {
+ console.error ("[WARN] in " + self.petal_name + ": " + e);
+ }
};
this.hub.event_manager.subscribe (this.petal_name, this.id, type, wrapped_handler);
};

0 comments on commit c4dd5f3

Please sign in to comment.