2013-05-26 22:51:58 +04:00
|
|
|
/*globals describe, beforeEach, it*/
|
2013-09-06 20:07:25 +04:00
|
|
|
var testUtils = require('./testUtils'),
|
|
|
|
should = require('should'),
|
2013-06-25 15:43:15 +04:00
|
|
|
when = require('when'),
|
|
|
|
sinon = require('sinon'),
|
2013-09-06 20:07:25 +04:00
|
|
|
|
|
|
|
// Stuff we are testing
|
2013-09-15 15:11:47 +04:00
|
|
|
colors = require("colors"),
|
2013-07-16 22:57:19 +04:00
|
|
|
errors = require('../../server/errorHandling'),
|
|
|
|
// storing current environment
|
|
|
|
currentEnv = process.env.NODE_ENV;
|
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
describe("Error handling", function () {
|
2013-05-26 22:51:58 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
// Just getting rid of jslint unused error
|
|
|
|
should.exist(errors);
|
2013-05-26 22:51:58 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
it("throws error objects", function () {
|
|
|
|
var toThrow = new Error("test1"),
|
|
|
|
runThrowError = function () {
|
|
|
|
errors.throwError(toThrow);
|
|
|
|
};
|
2013-05-26 22:51:58 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
runThrowError.should['throw']("test1");
|
|
|
|
});
|
2013-05-26 22:51:58 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
it("throws error strings", function () {
|
|
|
|
var toThrow = "test2",
|
|
|
|
runThrowError = function () {
|
|
|
|
errors.throwError(toThrow);
|
|
|
|
};
|
2013-05-26 22:51:58 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
runThrowError.should['throw']("test2");
|
|
|
|
});
|
2013-05-26 22:51:58 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
it("throws error even if nothing passed", function () {
|
|
|
|
var runThrowError = function () {
|
|
|
|
errors.throwError();
|
|
|
|
};
|
2013-05-26 22:51:58 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
runThrowError.should['throw']("An error occurred");
|
|
|
|
});
|
2013-05-26 22:51:58 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
it("logs errors", function () {
|
|
|
|
var err = new Error("test1"),
|
2013-09-19 06:50:06 +04:00
|
|
|
logStub = sinon.stub(console, "error");
|
2013-05-27 23:27:41 +04:00
|
|
|
|
2013-07-16 22:57:19 +04:00
|
|
|
// give environment a value that will console log
|
|
|
|
process.env.NODE_ENV = "development";
|
2013-06-25 15:43:15 +04:00
|
|
|
errors.logError(err);
|
2013-05-27 23:27:41 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
// Calls log with message on Error objects
|
2013-09-15 15:11:47 +04:00
|
|
|
logStub.calledWith("\nERROR:".red, err.message.red).should.equal(true);
|
2013-05-26 22:51:58 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
logStub.reset();
|
2013-05-26 22:51:58 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
err = "test2";
|
2013-05-26 22:51:58 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
errors.logError(err);
|
2013-05-26 22:51:58 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
// Calls log with string on strings
|
2013-09-15 15:11:47 +04:00
|
|
|
logStub.calledWith("\nERROR:".red, err.red).should.equal(true);
|
2013-05-26 22:51:58 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
logStub.restore();
|
2013-07-16 22:57:19 +04:00
|
|
|
process.env.NODE_ENV = currentEnv;
|
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
});
|
2013-05-26 22:51:58 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
it("logs promise errors and redirects", function (done) {
|
|
|
|
var def = when.defer(),
|
|
|
|
prom = def.promise,
|
|
|
|
req = null,
|
|
|
|
res = {
|
|
|
|
redirect: function () {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
},
|
2013-09-19 06:50:06 +04:00
|
|
|
logStub = sinon.stub(console, "error"),
|
2013-06-25 15:43:15 +04:00
|
|
|
redirectStub = sinon.stub(res, "redirect");
|
|
|
|
|
2013-07-16 22:57:19 +04:00
|
|
|
// give environment a value that will console log
|
|
|
|
process.env.NODE_ENV = "development";
|
2013-06-25 15:43:15 +04:00
|
|
|
prom.then(function () {
|
|
|
|
throw new Error("Ran success handler");
|
2013-09-15 15:11:47 +04:00
|
|
|
}, errors.logErrorWithRedirect("test1", null, null, "/testurl", req, res));
|
2013-06-25 15:43:15 +04:00
|
|
|
|
|
|
|
prom.otherwise(function () {
|
2013-09-15 15:11:47 +04:00
|
|
|
logStub.calledWith("\nERROR:".red, "test1".red).should.equal(true);
|
2013-06-25 15:43:15 +04:00
|
|
|
logStub.restore();
|
2013-05-26 22:51:58 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
redirectStub.calledWith('/testurl').should.equal(true);
|
|
|
|
redirectStub.restore();
|
2013-05-26 22:51:58 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
done();
|
2013-05-26 22:51:58 +04:00
|
|
|
});
|
2013-07-16 22:57:19 +04:00
|
|
|
prom.ensure(function () {
|
|
|
|
// gives the environment the correct value back
|
|
|
|
process.env.NODE_ENV = currentEnv;
|
|
|
|
});
|
2013-06-25 15:43:15 +04:00
|
|
|
def.reject();
|
2013-05-26 22:51:58 +04:00
|
|
|
});
|
2013-06-25 15:43:15 +04:00
|
|
|
});
|