Permalink
Browse files

[api fix] Be far more liberal in `errs.create` for truthy and falsy v…

…alues`.
  • Loading branch information...
indexzero committed Mar 7, 2013
1 parent 35b10e0 commit 20c1c76e2b9225c56bb7727b3302800ecf38ed2d
Showing with 44 additions and 13 deletions.
  1. +20 −13 lib/errs.js
  2. +24 −0 test/errs-test.js
View
@@ -144,27 +144,34 @@ exports.merge = function (err, type, opts) {
//
// optional stuff that might be created by module
//
- Object.keys(err).forEach(function (key) {
- //
- // in node v0.4 v8 errors where treated differently
- // we need to make sure we aren't merging these properties
- // http://code.google.com/p/v8/issues/detail?id=1215
- //
- if (['stack', 'type', 'arguments', 'message'].indexOf(key)===-1) {
- merged[key] = err[key];
- }
- });
+ if (!Array.isArray(err) && typeof err === 'object') {
+ Object.keys(err).forEach(function (key) {
+ //
+ // in node v0.4 v8 errors where treated differently
+ // we need to make sure we aren't merging these properties
+ // http://code.google.com/p/v8/issues/detail?id=1215
+ //
+ if (['stack', 'type', 'arguments', 'message'].indexOf(key)===-1) {
+ merged[key] = err[key];
+ }
+ });
+ }
// merging
merged.name = merged.name || err.name;
merged.message = merged.message || err.message;
// override stack
- merged.stack = err.stack;
+ merged.stack = err.stack || merged.stack;
// add human-readable errors
- merged.description = err.message;
- merged.stacktrace = (err.stack || '').split("\n");
+ if (err.message) {
+ merged.description = err.message;
+ }
+
+ if (err.stack && err.stack.split) {
+ merged.stacktrace = err.stack.split("\n");
+ }
return merged;
};
View
@@ -149,6 +149,30 @@ vows.describe('errs').addBatch({
var err = new Error('Msg!');
err = errs.merge(err, {ns: "test"});
assert.equal(err.ns, "test");
+ },
+ "with a truthy value": function () {
+ var err = errs.merge(true, {
+ message: 'Override!',
+ ns: 'lolwut'
+ })
+ assert.equal(err.message, 'Override!');
+ assert.equal(err.ns, 'lolwut');
+ },
+ "with a truthy stack": function () {
+ var err = errs.merge({ stack: true } , {
+ message: 'Override!',
+ ns: 'lolwut'
+ })
+ assert.equal(err.message, 'Override!');
+ assert.equal(err.ns, 'lolwut');
+ },
+ "with an Array stack": function () {
+ var err = errs.merge({ stack: [] } , {
+ message: 'Override!',
+ ns: 'lolwut'
+ })
+ assert.equal(err.message, 'Override!');
+ assert.equal(err.ns, 'lolwut');
}
}
}

0 comments on commit 20c1c76

Please sign in to comment.