Comparing version 1.4.0 to 1.5.0
var extsprintf = require('extsprintf'); | ||
var fs = require('fs'); | ||
var verror = require('../lib/verror'); | ||
var VError = require('../lib/verror'); | ||
@@ -9,3 +9,3 @@ function checkFile(filename, callback) { | ||
/* Annotate the "stat" error with what we were doing. */ | ||
return (callback(new verror.VError(err, | ||
return (callback(new VError(err, | ||
'failed to check "%s"', filename))); | ||
@@ -22,3 +22,3 @@ | ||
/* Annotate the "checkFile" error. */ | ||
return (callback(new verror.VError( | ||
return (callback(new VError( | ||
err, 'request failed'))); | ||
@@ -25,0 +25,0 @@ |
var extsprintf = require('extsprintf'); | ||
var fs = require('fs'); | ||
var verror = require('../lib/verror'); | ||
var VError = require('../lib/verror'); | ||
@@ -9,3 +9,3 @@ function checkFile(filename, callback) { | ||
/* Annotate the "stat" error with what we were doing. */ | ||
return (callback(new verror.VError(err, | ||
return (callback(new VError(err, | ||
'failed to check "%s"', filename))); | ||
@@ -22,3 +22,3 @@ | ||
/* Wrap the "checkFile" error. */ | ||
return (callback(new verror.WError( | ||
return (callback(new VError.WError( | ||
err, 'request failed'))); | ||
@@ -25,0 +25,0 @@ |
@@ -1,6 +0,6 @@ | ||
var verror = require('../lib/verror'); | ||
var VError = require('../lib/verror'); | ||
var opname = 'read'; | ||
var err = new verror.VError('"%s" operation failed', opname); | ||
var err = new VError('"%s" operation failed', opname); | ||
console.log(err.message); | ||
console.log(err.stack); |
@@ -6,5 +6,5 @@ var fs = require('fs'); | ||
fs.stat(filename, function (err1) { | ||
var err2 = new VError(err1, 'stat "%s"', filename); | ||
var err2 = new VError(err1, 'stat "%s" failed', filename); | ||
console.error(err2.message); | ||
console.error(err2.cause().message); | ||
}); |
@@ -19,2 +19,3 @@ /* | ||
/* Other exported classes */ | ||
VError.SError = SError; | ||
VError.WError = WError; | ||
@@ -48,2 +49,33 @@ VError.MultiError = MultiError; | ||
/* | ||
* extsprintf (which we invoke here with our caller's arguments in order | ||
* to construct this Error's message) is strict in its interpretation of | ||
* values to be processed by the "%s" specifier. The value passed to | ||
* extsprintf must actually be a string or something convertible to a | ||
* String using .toString(). Passing other values (notably "null" and | ||
* "undefined") is considered a programmer error. The assumption is | ||
* that if you actually want to print the string "null" or "undefined", | ||
* then that's easy to do that when you're calling extsprintf; on the | ||
* other hand, if you did NOT want that (i.e., there's actually a bug | ||
* where the program assumes some variable is non-null and tries to | ||
* print it, which might happen when constructing a packet or file in | ||
* some specific format), then it's better to stop immediately than | ||
* produce bogus output. | ||
* | ||
* However, sometimes the bug is only in the code calling VError, and a | ||
* programmer might prefer to have the error message contain "null" or | ||
* "undefined" rather than have the bug in the error path crash the | ||
* program (making the first bug harder to identify). For that reason, | ||
* by default VError converts "null" or "undefined" arguments to their | ||
* string representations and passes those to extsprintf. Programmers | ||
* desiring the strict behavior can use the SError class or pass the | ||
* "strict" option to the VError constructor. | ||
*/ | ||
if (!options || !options.strict) { | ||
args = args.map(function (a) { | ||
return (a === null ? 'null' : | ||
a === undefined ? 'undefined' : a); | ||
}); | ||
} | ||
tailmsg = args.length > 0 ? | ||
@@ -96,2 +128,34 @@ mod_extsprintf.sprintf.apply(null, args) : ''; | ||
/* | ||
* SError is like VError, but stricter about types. You cannot pass "null" or | ||
* "undefined" as string arguments to the formatter. Since SError is only a | ||
* different function, not really a different class, we don't set | ||
* SError.prototype.name. | ||
*/ | ||
function SError() | ||
{ | ||
var fmtargs, opts, key, args; | ||
opts = {}; | ||
opts.constructorOpt = SError; | ||
if (arguments[0] instanceof Error) { | ||
opts.cause = arguments[0]; | ||
fmtargs = Array.prototype.slice.call(arguments, 1); | ||
} else if (typeof (arguments[0]) == 'object') { | ||
for (key in arguments[0]) | ||
opts[key] = arguments[0][key]; | ||
fmtargs = Array.prototype.slice.call(arguments, 1); | ||
} else { | ||
fmtargs = Array.prototype.slice.call(arguments, 0); | ||
} | ||
opts.strict = true; | ||
args = [ opts ].concat(fmtargs); | ||
VError.apply(this, args); | ||
} | ||
mod_util.inherits(SError, VError); | ||
/* | ||
* Represents a collection of errors for the purpose of consumers that generally | ||
@@ -114,3 +178,2 @@ * only deal with one error. Callers can extract the individual errors | ||
/* | ||
@@ -117,0 +180,0 @@ * Like JavaScript's built-in Error class, but supports a "cause" argument which |
{ | ||
"name": "verror", | ||
"version": "1.4.0", | ||
"version": "1.5.0", | ||
"description": "richer JavaScript errors", | ||
@@ -5,0 +5,0 @@ "main": "./lib/verror.js", |
@@ -56,3 +56,3 @@ # verror: richer JavaScript errors | ||
fs.stat(filename, function (err1) { | ||
var err2 = new VError(err1, 'stat "%s"', filename); | ||
var err2 = new VError(err1, 'stat "%s" failed', filename); | ||
console.error(err2.message); | ||
@@ -64,3 +64,3 @@ }); | ||
stat failed: ENOENT, stat '/nonexistent' | ||
stat "/nonexistent" failed: ENOENT, stat '/nonexistent' | ||
@@ -67,0 +67,0 @@ which resembles how Unix programs typically report errors: |
@@ -8,6 +8,4 @@ /* | ||
var mod_verror = require('../lib/verror'); | ||
var VError = mod_verror.VError; | ||
var WError = mod_verror.WError; | ||
var VError = require('../lib/verror'); | ||
var WError = VError.WError; | ||
var err, suberr; | ||
@@ -14,0 +12,0 @@ |
@@ -157,1 +157,7 @@ /* | ||
].join('\n') + '\n' + nodestack); | ||
/* "null" or "undefined" as string for extsprintf */ | ||
err = new VError('my %s string', null); | ||
mod_assert.equal('my null string', err.message); | ||
err = new VError('my %s string', undefined); | ||
mod_assert.equal('my undefined string', err.message); |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
50214
20
788