Skip to content

Commit

Permalink
Log an error when trying to reject an async validator
Browse files Browse the repository at this point in the history
  • Loading branch information
ansman committed Jul 3, 2015
1 parent 6c12536 commit 237d5a2
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 20 deletions.
18 changes: 9 additions & 9 deletions specs/validate-async-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ describe("validate.async", function() {
success = jasmine.createSpy("success");
error = jasmine.createSpy("error");

validate.validators.asyncFail = function() {
validate.validators.asyncFailReject = function() {
return new validate.Promise(function(resolve, reject) {
setTimeout(function() {
reject("failz");
}, 1);
});
};

validate.validators.asyncFailResolve = function() {
validate.validators.asyncFail = function() {
return new validate.Promise(function(resolve, reject) {
setTimeout(function() {
resolve("failz");
Expand All @@ -33,8 +33,8 @@ describe("validate.async", function() {
});

afterEach(function() {
delete validate.validators.asyncFailReject;
delete validate.validators.asyncFail;
delete validate.validators.asyncFailResolve;
delete validate.validators.asyncSuccess;
delete validate.validators.asyncError;
delete validate.async.options;
Expand Down Expand Up @@ -96,7 +96,7 @@ describe("validate.async", function() {
it.promise('handles validators resolving a promise with error', function() {
var c = {
name: {
asyncFailResolve: true
asyncFail: true
}
};
return validate.async({}, c).then(success, error).then(function() {
Expand Down Expand Up @@ -165,20 +165,20 @@ describe("validate.async", function() {
});
});

it.promise("warns if a promise is rejected without an error", function() {
spyOn(validate, "warn");
it.promise("still works with rejecting with an error but logs an error", function() {
spyOn(validate, "error");

var results = [{
attribute: "foo",
error: new validate.Promise(function(resolve, reject) { reject(); })
error: new validate.Promise(function(resolve, reject) { reject("foo"); })
}];

return validate.waitForResults(results).then(function() {
expect(results).toEqual([{
attribute: "foo",
error: undefined
error: "foo"
}]);
expect(validate.warn).toHaveBeenCalled();
expect(validate.error).toHaveBeenCalled();
});
});

Expand Down
4 changes: 2 additions & 2 deletions specs/validate-helpers-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -667,7 +667,7 @@ describe("validate", function() {
warn: jasmine.createSpy("warn")
};
validate.warn("Msg");
expect(window.console.warn).toHaveBeenCalledWith("Msg");
expect(window.console.warn).toHaveBeenCalledWith("[validate.js] Msg");
});
});

Expand All @@ -687,7 +687,7 @@ describe("validate", function() {
error: jasmine.createSpy("error")
};
validate.error("Msg");
expect(window.console.error).toHaveBeenCalledWith("Msg");
expect(window.console.error).toHaveBeenCalledWith("[validate.js] Msg");
});
});

Expand Down
16 changes: 7 additions & 9 deletions validate.js
Original file line number Diff line number Diff line change
Expand Up @@ -227,17 +227,15 @@

return memo.then(function() {
return result.error.then(
function(validationResult) {
result.error = validationResult || null;
function(error) {
result.error = error || null;
},
function(error) {
// If for some reason the validator promise was rejected but no
// error was specified.
if (!error) {
v.warn("Validator promise was rejected but didn't return an error");
} else if (error instanceof Error) {
if (error instanceof Error) {
throw error;
}
console.log("Foo");
v.error("[validate.js] Rejecting promises with the result is deprecated. Please use the resolve callback instead.");
result.error = error;
}
);
Expand Down Expand Up @@ -719,13 +717,13 @@

warn: function(msg) {
if (typeof console !== "undefined" && console.warn) {
console.warn(msg);
console.warn("[validate.js] " + msg);
}
},

error: function(msg) {
if (typeof console !== "undefined" && console.error) {
console.error(msg);
console.error("[validate.js] " + msg);
}
}
});
Expand Down

0 comments on commit 237d5a2

Please sign in to comment.