mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-04-08 02:52:39 -05:00
Merge pull request #2469 from ErisDS/error-cleanup
logError & logWarn call console.log once
This commit is contained in:
commit
f981e71e3c
2 changed files with 68 additions and 90 deletions
|
@ -47,23 +47,27 @@ errors = {
|
|||
process.env.NODE_ENV === 'staging' ||
|
||||
process.env.NODE_ENV === 'production')) {
|
||||
|
||||
console.log('\nWarning:'.yellow, warn.yellow);
|
||||
var msgs = ['\nWarning:'.yellow, warn.yellow, '\n'];
|
||||
|
||||
if (context) {
|
||||
console.log(context.white);
|
||||
msgs.push(context.white, '\n');
|
||||
}
|
||||
|
||||
if (help) {
|
||||
console.log(help.green);
|
||||
msgs.push(help.green);
|
||||
}
|
||||
|
||||
// add a new line
|
||||
console.log('');
|
||||
msgs.push('\n');
|
||||
|
||||
console.log.apply(console, msgs);
|
||||
}
|
||||
},
|
||||
|
||||
logError: function (err, context, help) {
|
||||
var stack = err ? err.stack : null;
|
||||
var stack = err ? err.stack : null,
|
||||
msgs;
|
||||
|
||||
if (err) {
|
||||
err = err.message || err || 'An unknown error occurred.';
|
||||
} else {
|
||||
|
@ -75,22 +79,24 @@ errors = {
|
|||
process.env.NODE_ENV === 'staging' ||
|
||||
process.env.NODE_ENV === 'production')) {
|
||||
|
||||
console.error('\nERROR:'.red, err.red);
|
||||
msgs = ['\nERROR:'.red, err.red, '\n'];
|
||||
|
||||
if (context) {
|
||||
console.error(context.white);
|
||||
msgs.push(context.white, '\n');
|
||||
}
|
||||
|
||||
if (help) {
|
||||
console.error(help.green);
|
||||
msgs.push(help.green);
|
||||
}
|
||||
|
||||
// add a new line
|
||||
console.error('');
|
||||
msgs.push('\n');
|
||||
|
||||
if (stack) {
|
||||
console.error(stack, '\n');
|
||||
msgs.push(stack, '\n');
|
||||
}
|
||||
|
||||
console.error.apply(console, msgs);
|
||||
}
|
||||
},
|
||||
|
||||
|
|
|
@ -3,16 +3,15 @@ var testUtils = require('../utils'),
|
|||
should = require('should'),
|
||||
when = require('when'),
|
||||
sinon = require('sinon'),
|
||||
express = require("express"),
|
||||
express = require('express'),
|
||||
|
||||
// Stuff we are testing
|
||||
colors = require("colors"),
|
||||
colors = require('colors'),
|
||||
errors = require('../../server/errorHandling'),
|
||||
// storing current environment
|
||||
currentEnv = process.env.NODE_ENV,
|
||||
ONE_HOUR_S = 60 * 60;
|
||||
currentEnv = process.env.NODE_ENV;
|
||||
|
||||
describe("Error handling", function () {
|
||||
describe('Error handling', function () {
|
||||
|
||||
// Just getting rid of jslint unused error
|
||||
should.exist(errors);
|
||||
|
@ -49,7 +48,7 @@ describe("Error handling", function () {
|
|||
var logStub;
|
||||
|
||||
beforeEach(function () {
|
||||
logStub = sinon.stub(console, "error");
|
||||
logStub = sinon.stub(console, 'error');
|
||||
// give environment a value that will console log
|
||||
process.env.NODE_ENV = "development";
|
||||
});
|
||||
|
@ -60,142 +59,115 @@ describe("Error handling", function () {
|
|||
process.env.NODE_ENV = currentEnv;
|
||||
});
|
||||
|
||||
it("logs errors from error objects", function () {
|
||||
var err = new Error("test1");
|
||||
it('logs errors from error objects', function () {
|
||||
var err = new Error('test1');
|
||||
|
||||
errors.logError(err);
|
||||
|
||||
// Calls log with message on Error objects
|
||||
logStub.calledThrice.should.be.true;
|
||||
logStub.firstCall.calledWith("\nERROR:".red, err.message.red).should.be.true;
|
||||
logStub.secondCall.calledWith('').should.be.true;
|
||||
logStub.thirdCall.calledWith(err.stack, '\n').should.be.true;
|
||||
|
||||
logStub.calledOnce.should.be.true;
|
||||
logStub.calledWith('\nERROR:'.red, err.message.red, '\n', '\n', err.stack, '\n').should.be.true;
|
||||
});
|
||||
|
||||
it("logs errors from strings", function () {
|
||||
var err = "test2";
|
||||
it('logs errors from strings', function () {
|
||||
var err = 'test2';
|
||||
|
||||
errors.logError(err);
|
||||
|
||||
// Calls log with string on strings
|
||||
logStub.calledTwice.should.be.true;
|
||||
logStub.firstCall.calledWith("\nERROR:".red, err.red).should.be.true;
|
||||
logStub.secondCall.calledWith('').should.be.true;
|
||||
logStub.calledOnce.should.be.true;
|
||||
logStub.calledWith('\nERROR:'.red, err.red, '\n').should.be.true;
|
||||
});
|
||||
|
||||
it("logs errors from an error object and two string arguments", function () {
|
||||
var err = new Error("test1"),
|
||||
message = "Testing";
|
||||
it('logs errors from an error object and two string arguments', function () {
|
||||
var err = new Error('test1'),
|
||||
message = 'Testing';
|
||||
|
||||
errors.logError(err, message, message);
|
||||
|
||||
// Calls log with message on Error objects
|
||||
|
||||
logStub.callCount.should.equal(5);
|
||||
logStub.calledWith("\nERROR:".red, err.message.red).should.be.true;
|
||||
logStub.firstCall.calledWith("\nERROR:".red, err.message.red).should.be.true;
|
||||
logStub.secondCall.calledWith(message.white).should.be.true;
|
||||
logStub.thirdCall.calledWith(message.green).should.be.true;
|
||||
logStub.getCall(3).calledWith('').should.be.true; //nth call uses zero-based numbering
|
||||
logStub.lastCall.calledWith(err.stack, '\n').should.be.true;
|
||||
logStub.calledOnce.should.be.true;
|
||||
logStub.calledWith('\nERROR:'.red, err.message.red, '\n', message.white, '\n', message.green, '\n', err.stack, '\n');
|
||||
});
|
||||
|
||||
it("logs errors from three string arguments", function () {
|
||||
var message = "Testing";
|
||||
it('logs errors from three string arguments', function () {
|
||||
var message = 'Testing';
|
||||
|
||||
errors.logError(message, message, message);
|
||||
|
||||
// Calls log with message on Error objects
|
||||
|
||||
logStub.callCount.should.equal(4);
|
||||
logStub.firstCall.calledWith("\nERROR:".red, message.red).should.be.true;
|
||||
logStub.secondCall.calledWith(message.white).should.be.true;
|
||||
logStub.thirdCall.calledWith(message.green).should.be.true;
|
||||
logStub.lastCall.calledWith('').should.be.true;
|
||||
logStub.calledOnce.should.be.true;
|
||||
logStub.calledWith('\nERROR:'.red, message.red, '\n', message.white, '\n', message.green, '\n').should.be.true;
|
||||
});
|
||||
|
||||
it("logs errors from an undefined error argument", function () {
|
||||
var message = "Testing";
|
||||
it('logs errors from an undefined error argument', function () {
|
||||
var message = 'Testing';
|
||||
|
||||
errors.logError(undefined, message, message);
|
||||
|
||||
// Calls log with message on Error objects
|
||||
|
||||
logStub.callCount.should.equal(4);
|
||||
logStub.firstCall.calledWith("\nERROR:".red, "An unknown error occurred.".red).should.be.true;
|
||||
logStub.secondCall.calledWith(message.white).should.be.true;
|
||||
logStub.thirdCall.calledWith(message.green).should.be.true;
|
||||
logStub.lastCall.calledWith('').should.be.true;
|
||||
logStub.calledOnce.should.be.true;
|
||||
logStub.calledWith('\nERROR:'.red, 'An unknown error occurred.'.red, '\n', message.white, '\n', message.green , '\n').should.be.true;
|
||||
});
|
||||
|
||||
it("logs errors from an undefined context argument", function () {
|
||||
var message = "Testing";
|
||||
it('logs errors from an undefined context argument', function () {
|
||||
var message = 'Testing';
|
||||
|
||||
errors.logError(message, undefined, message);
|
||||
|
||||
// Calls log with message on Error objects
|
||||
|
||||
logStub.callCount.should.equal(3);
|
||||
logStub.firstCall.calledWith("\nERROR:".red, message.red).should.be.true;
|
||||
logStub.secondCall.calledWith(message.green).should.be.true;
|
||||
logStub.lastCall.calledWith('').should.be.true;
|
||||
logStub.calledOnce.should.be.true;
|
||||
logStub.calledWith('\nERROR:'.red, message.red, '\n', message.green, '\n').should.be.true;
|
||||
});
|
||||
|
||||
it("logs errors from an undefined help argument", function () {
|
||||
var message = "Testing";
|
||||
it('logs errors from an undefined help argument', function () {
|
||||
var message = 'Testing';
|
||||
|
||||
errors.logError(message, message, undefined);
|
||||
|
||||
// Calls log with message on Error objects
|
||||
|
||||
logStub.callCount.should.equal(3);
|
||||
logStub.firstCall.calledWith("\nERROR:".red, message.red).should.be.true;
|
||||
logStub.secondCall.calledWith(message.white).should.be.true;
|
||||
logStub.lastCall.calledWith('').should.be.true;
|
||||
logStub.calledOnce.should.be.true;
|
||||
logStub.calledWith('\nERROR:'.red, message.red, '\n', message.white, '\n').should.be.true;
|
||||
});
|
||||
|
||||
it("logs errors from a null error argument", function () {
|
||||
var message = "Testing";
|
||||
it('logs errors from a null error argument', function () {
|
||||
var message = 'Testing';
|
||||
|
||||
errors.logError(null, message, message);
|
||||
|
||||
// Calls log with message on Error objects
|
||||
|
||||
logStub.callCount.should.equal(4);
|
||||
logStub.firstCall.calledWith("\nERROR:".red, "An unknown error occurred.".red).should.be.true;
|
||||
logStub.secondCall.calledWith(message.white).should.be.true;
|
||||
logStub.thirdCall.calledWith(message.green).should.be.true;
|
||||
logStub.lastCall.calledWith('').should.be.true;
|
||||
logStub.calledOnce.should.be.true;
|
||||
logStub.calledWith('\nERROR:'.red, 'An unknown error occurred.'.red, '\n', message.white, '\n', message.green, '\n').should.be.true;
|
||||
});
|
||||
|
||||
it("logs errors from a null context argument", function () {
|
||||
var message = "Testing";
|
||||
it('logs errors from a null context argument', function () {
|
||||
var message = 'Testing';
|
||||
|
||||
errors.logError(message, null, message);
|
||||
|
||||
// Calls log with message on Error objects
|
||||
|
||||
logStub.callCount.should.equal(3);
|
||||
logStub.firstCall.calledWith("\nERROR:".red, message.red).should.be.true;
|
||||
logStub.secondCall.calledWith(message.green).should.be.true;
|
||||
logStub.lastCall.calledWith('').should.be.true;
|
||||
logStub.calledOnce.should.be.true;
|
||||
logStub.firstCall.calledWith('\nERROR:'.red, message.red, '\n', message.green, '\n').should.be.true;
|
||||
});
|
||||
|
||||
it("logs errors from a null help argument", function () {
|
||||
var message = "Testing";
|
||||
it('logs errors from a null help argument', function () {
|
||||
var message = 'Testing';
|
||||
|
||||
errors.logError(message, message, null);
|
||||
|
||||
// Calls log with message on Error objects
|
||||
|
||||
logStub.callCount.should.equal(3);
|
||||
logStub.firstCall.calledWith("\nERROR:".red, message.red).should.be.true;
|
||||
logStub.secondCall.calledWith(message.white).should.be.true;
|
||||
logStub.lastCall.calledWith('').should.be.true;
|
||||
logStub.calledOnce.should.be.true;
|
||||
logStub.firstCall.calledWith('\nERROR:'.red, message.red, '\n', message.white, '\n').should.be.true;
|
||||
|
||||
});
|
||||
|
||||
it("logs promise errors and redirects", function (done) {
|
||||
it('logs promise errors and redirects', function (done) {
|
||||
var def = when.defer(),
|
||||
prom = def.promise,
|
||||
req = null,
|
||||
|
@ -204,15 +176,15 @@ describe("Error handling", function () {
|
|||
return;
|
||||
}
|
||||
},
|
||||
redirectStub = sinon.stub(res, "redirect");
|
||||
redirectStub = sinon.stub(res, 'redirect');
|
||||
|
||||
// give environment a value that will console log
|
||||
prom.then(function () {
|
||||
throw new Error("Ran success handler");
|
||||
}, errors.logErrorWithRedirect("test1", null, null, "/testurl", req, res));
|
||||
throw new Error('Ran success handler');
|
||||
}, errors.logErrorWithRedirect('test1', null, null, '/testurl', req, res));
|
||||
|
||||
prom.otherwise(function () {
|
||||
logStub.calledWith("\nERROR:".red, "test1".red).should.equal(true);
|
||||
logStub.calledWith('\nERROR:'.red, 'test1'.red).should.equal(true);
|
||||
logStub.restore();
|
||||
|
||||
redirectStub.calledWith('/testurl').should.equal(true);
|
||||
|
|
Loading…
Add table
Reference in a new issue