Skip to content
This repository has been archived by the owner on Mar 10, 2020. It is now read-only.

Commit

Permalink
Correctly handle bad JSONs in JSONAscoltatore.
Browse files Browse the repository at this point in the history
  • Loading branch information
mcollina committed Mar 21, 2014
1 parent aff537c commit a1ee583
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
11 changes: 9 additions & 2 deletions lib/json_ascoltatore.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,17 @@ JSONAscoltatore.prototype.wrapCallback = function(callback, next) {
if (!callback._json_ascoltatore_wrapper) {
callback._json_ascoltatore_wrapper = function(t, payload) {
debug("converting from JSON");
var errored = false;

try {
callback(t, JSON.parse(payload));
payload = JSON.parse(payload);
} catch (e) {
console.log("The payload is not a valid JSON", e);
errored = true;
debug("The payload is not a valid JSON", e);
}

if (!errored) {
callback(t, payload);
}
};
}
Expand Down
11 changes: 11 additions & 0 deletions test/json_ascoltatore_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,15 @@ describe("ascoltatori.JSONAscoltatore", function() {
that.instance.pub("hello/123", false);
});
});

it("should not throw if a bad JSON arrives", function(done) {
var that = this;
this.instance.subscribe("/hello", function(topic, payload) {
done(new Error("this should never happen"));
}, function() {
that.included.publish("/hello", "not a json", function() {
done();
});
});
});
});

0 comments on commit a1ee583

Please sign in to comment.