Permalink
Browse files

[dist] 100% full-compliance with JSHint settings: https://gist.github…

  • Loading branch information...
1 parent 0e85929 commit bbc7f29058cda876a9000d75c788bebf04b163f0 @indexzero committed Mar 3, 2012
Showing with 41 additions and 38 deletions.
  1. +40 −37 lib/errs.js
  2. +1 −1 test/macros.js
View
@@ -5,7 +5,7 @@
* MIT LICENSE
*
*/
-
+
var events = require('events'),
util = require('util');
@@ -23,79 +23,79 @@ exports.registered = {};
// * `array`: Message for the error (' ' joined).
// * `function`: Function to return error options.
//
-// Creates a new error instance for with the specified `type`
-// and `options`. If the `type` is not registered then a new
+// Creates a new error instance for with the specified `type`
+// and `options`. If the `type` is not registered then a new
// `Error` instance will be created.
//
-exports.create = function (type, opts) {
+exports.create = function createErr(type, opts) {
if (!arguments[1] && !exports.registered[type]) {
opts = type;
type = null;
}
-
+
//
// If the `opts` has a `stack` property assume
// that it is already an error instance.
//
if (opts && opts.stack) {
return opts;
}
-
+
var message,
- errorProto,
+ ErrorProto,
error;
-
+
//
// Parse arguments liberally for the message
//
if (typeof opts === 'function') {
opts = opts();
}
-
+
if (Array.isArray(opts)) {
message = opts.join(' ');
opts = null;
}
else if (opts) {
switch (typeof opts) {
- case 'string':
- message = opts || 'Unspecified error';
+ case 'string':
+ message = opts || 'Unspecified error';
opts = null;
break;
- case 'object':
+ case 'object':
message = (opts && opts.message) || 'Unspecified error';
break;
default:
message = 'Unspecified error';
break;
- }
+ }
}
-
+
//
// Instantiate a new Error instance or a new
- // registered error type (if it exists).
+ // registered error type (if it exists).
//
- errorProto = type && exports.registered[type] || Error;
- error = new (errorProto)(message);
-
+ ErrorProto = type && exports.registered[type] || Error;
+ error = new (ErrorProto)(message);
+
if (!error.name || error.name === 'Error') {
- error.name = errorProto.name || 'Error';
+ error.name = ErrorProto.name || 'Error';
}
-
+
//
- // Capture a stack trace if it does not already exist and
+ // Capture a stack trace if it does not already exist and
// remote the part of the stack trace referencing `errs.js`.
//
if (!error.stack) {
Error.call(error);
- Error.captureStackTrace(error, arguments.callee);
+ Error.captureStackTrace(error, createErr);
}
else {
error.stack = error.stack.split('\n');
error.stack.splice(1, 1);
error.stack = error.stack.join('\n');
- }
-
+ }
+
//
// Copy all options to the new error instance.
//
@@ -104,7 +104,7 @@ exports.create = function (type, opts) {
error[key] = opts[key];
});
}
-
+
return error;
};
@@ -118,25 +118,28 @@ exports.create = function (type, opts) {
// * `array`: Message for the error (' ' joined).
// * `function`: Function to return error options.
//
-// Merges an existing error with a new error instance for with
+// Merges an existing error with a new error instance for with
// the specified `type` and `options`.
//
exports.merge = function (err, type, opts) {
- if (!arguments[1] && !exports.registered[type]) {
- opts = type;
- type = null;
- }
- var merged = exports.create(type,opts);
+ var merged = exports.create(type, opts);
+
// optional stuff that might be created by module
- for (var property in err) { merged[property] = err[property]; }
+ Object.keys(err).forEach(function (key) {
+ merged[key] = err[key];
+ });
+
// merging
merged.name = merged.name || err.name;
merged.message = merged.message || err.message;
+
// override stack
merged.stack = err.stack;
+
// add human-readable errors
merged.description = err.message;
merged.stacktrace = err.stack.split("\n");
+
return merged;
};
@@ -146,19 +149,19 @@ exports.merge = function (err, type, opts) {
// #### @callback {function} **Optional** Continuation to pass the error to.
//
// Attempts to instantiate the given `error`. If the `error` is already a properly
-// formed `error` object (with a `stack` property) it will not be modified.
+// formed `error` object (with a `stack` property) it will not be modified.
//
// * If a `callback` is supplied, it is invoked with the `error`.
-// * If no `callback`, return a new `EventEmitter` which emits `error`
+// * If no `callback`, return a new `EventEmitter` which emits `error`
// on `process.nextTick()`.
//
exports.handle = function (error, callback) {
error = exports.create(error);
-
+
if (callback) {
return callback(error);
- }
-
+ }
+
var emitter = new events.EventEmitter();
process.nextTick(function () { emitter.emit('error', error); });
return emitter;
View
@@ -88,4 +88,4 @@ macros.create.registered = function (type, proto, obj) {
});
}
};
-}
+};

0 comments on commit bbc7f29

Please sign in to comment.