Skip to content

Commit

Permalink
Move coerceError to cuvva-error.js
Browse files Browse the repository at this point in the history
  • Loading branch information
jFransham committed Sep 1, 2015
1 parent 763ff0b commit b0e59cf
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 26 deletions.
18 changes: 0 additions & 18 deletions lib/coerce-error.js

This file was deleted.

20 changes: 20 additions & 0 deletions lib/cuvva-error.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,23 @@ CuvvaError.httpStatusMap = {};

util.inherits(CuvvaError, Error);
module.exports = CuvvaError;

module.exports.coerceError = function (error) {
if (error instanceof CuvvaError)
return error;

var newError;

if (error instanceof Error) {
newError = new CuvvaError('unknown', { message: error.message });
Object.defineProperty(newError, 'stack', { get: function() { return error.stack } });
} else {
newError = new CuvvaError('unknown', { error: error });
}

if (error.statusCode)
newError.httpStatus = error.statusCode;

return newError;
};

2 changes: 1 addition & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,5 +83,5 @@ module.exports.warn = logger('warn');
module.exports.error = logger('error');
module.exports.fatal = logger('fatal');

module.exports.coerceError = require('./coerce-error.js');
module.exports.coerceError = CuvvaError.coerceError;

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "cuvva-log",
"version": "0.5.2",
"version": "0.5.3",
"description": "Super simple standardized logging used by all Cuvva systems",
"homepage": "https://github.com/cuvva/cuvva-log-node",
"bugs": "https://github.com/cuvva/cuvva-log-node/issues",
Expand Down
12 changes: 6 additions & 6 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ test('log.debug() prints correctly', function (t) {
log.debug('test_error');
});

t.equals(err[0], 'debug:{"code":"test_error","httpStatus":500}\n');
t.equals(err[0], 'debug:{"code":"test_error"}\n');
t.end();
});

Expand All @@ -22,7 +22,7 @@ test('log.info() prints correctly', function (t) {
log.info('test_error');
});

t.equals(err[0], 'info:{"code":"test_error","httpStatus":500}\n');
t.equals(err[0], 'info:{"code":"test_error"}\n');
t.end();
});

Expand All @@ -31,7 +31,7 @@ test('log.warn() prints correctly', function (t) {
log.warn('test_error');
});

t.equals(err[0], 'warn:{"code":"test_error","httpStatus":500}\n');
t.equals(err[0], 'warn:{"code":"test_error"}\n');
t.end();
});

Expand All @@ -40,7 +40,7 @@ test('log.error() prints correctly', function (t) {
log.error('test_error');
});

t.equals(err[0], 'error:{"code":"test_error","httpStatus":500}\n');
t.equals(err[0], 'error:{"code":"test_error"}\n');
t.end();
});

Expand All @@ -49,7 +49,7 @@ test('log.fatal() prints correctly', function (t) {
log.fatal('test_error');
});

t.equals(err[0], 'fatal:{"code":"test_error","httpStatus":500}\n');
t.equals(err[0], 'fatal:{"code":"test_error"}\n');
t.end();
});

Expand All @@ -58,6 +58,6 @@ test('log prints JSON correctly', function (t) {
log.error('test_error', { a: 'b', c: 'd' });
});

t.equals(err[0], 'error:{"code":"test_error","httpStatus":500,"meta":{"a":"b","c":"d"}}\n');
t.equals(err[0], 'error:{"code":"test_error","meta":{"a":"b","c":"d"}}\n');
t.end();
});

0 comments on commit b0e59cf

Please sign in to comment.