Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a sanitizeError option and specs #32

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions q-connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ function Connection(connection, local, options) {
var makeId = options.makeId || function () {
return UUID.generate();
};
var sanitizeError = typeof options.sanitizeError === "function" ? options.sanitizeError : false;
var locals = LruMap(null, options.max || Infinity);
connection = adapt(connection, options.origin);

Expand Down Expand Up @@ -243,6 +244,9 @@ function Connection(connection, local, options) {
) {
var result = {};
if (object instanceof Error) {
if (sanitizeError) {
object = sanitizeError(object);
}
result.message = object.message;
result.stack = object.stack;
}
Expand Down
50 changes: 47 additions & 3 deletions spec/q-connection-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ function makeChannel() {
};
}

function makePeers(local, remote) {
function makePeers(local, remote, options) {
var channel = makeChannel();
return {
local: Connection(channel.l2r, local),
remote: Connection(channel.r2l, remote),
local: Connection(channel.l2r, local, options),
remote: Connection(channel.r2l, remote, options),
close: channel.close
}
}
Expand Down Expand Up @@ -380,3 +380,47 @@ describe("serialization", function () {

});

describe("options", function () {
describe("sanitizeError", function () {
var peers, options;
beforeEach(function () {
options = {
sanitizeError: function (error) {
return {sanitized: true};
}
};
spyOn(options, "sanitizeError").andCallThrough();
peers = makePeers({
object: function () {
return {object: true};
},
error: function () {
return new Error("error");
}
}, null, options);
});

it("should be called with the error", function () {
return peers.remote.invoke("error")
.then(function (response) {
expect(options.sanitizeError).toHaveBeenCalled();
expect(options.sanitizeError.mostRecentCall.args[0].message).toEqual("error");
});
});

it("'s return value should be used instead of the error", function () {
return peers.remote.invoke("error")
.then(function (response) {
expect(response).toEqual({sanitized: true});
});
});

it("is not called for plain objects", function () {
return peers.remote.invoke("object")
.then(function (response) {
expect(response).toEqual({object: true});
expect(options.sanitizeError).not.toHaveBeenCalled();
});
});
});
});