2 * verror.js: richer JavaScript errors
5 var mod_assert = require('assert');
6 var mod_util = require('util');
8 var mod_extsprintf = require('extsprintf');
13 exports.VError = VError;
14 exports.WError = WError;
15 exports.MultiError = MultiError;
18 * Like JavaScript's built-in Error class, but supports a "cause" argument and a
19 * printf-style message. The cause argument can be null.
21 function VError(options)
23 var args, causedBy, ctor, tailmsg;
25 if (options instanceof Error || typeof (options) === 'object') {
26 args = Array.prototype.slice.call(arguments, 1);
28 args = Array.prototype.slice.call(arguments, 0);
32 tailmsg = args.length > 0 ?
33 mod_extsprintf.sprintf.apply(null, args) : '';
34 this.jse_shortmsg = tailmsg;
35 this.jse_summary = tailmsg;
38 causedBy = options.cause;
40 if (!causedBy || !(options.cause instanceof Error))
43 if (causedBy && (causedBy instanceof Error)) {
44 this.jse_cause = causedBy;
45 this.jse_summary += ': ' + causedBy.message;
49 this.message = this.jse_summary;
50 Error.call(this, this.jse_summary);
52 if (Error.captureStackTrace) {
53 ctor = options ? options.constructorOpt : undefined;
54 ctor = ctor || arguments.callee;
55 Error.captureStackTrace(this, ctor);
59 mod_util.inherits(VError, Error);
60 VError.prototype.name = 'VError';
62 VError.prototype.toString = function ve_toString()
64 var str = (this.hasOwnProperty('name') && this.name ||
65 this.constructor.name || this.constructor.prototype.name);
67 str += ': ' + this.message;
72 VError.prototype.cause = function ve_cause()
74 return (this.jse_cause);
79 * Represents a collection of errors for the purpose of consumers that generally
80 * only deal with one error. Callers can extract the individual errors
81 * contained in this object, but may also just treat it as a normal single
82 * error, in which case a summary message will be printed.
84 function MultiError(errors)
86 mod_assert.ok(errors.length > 0);
87 this.ase_errors = errors;
89 VError.call(this, errors[0], 'first of %d error%s',
90 errors.length, errors.length == 1 ? '' : 's');
93 mod_util.inherits(MultiError, VError);
98 * Like JavaScript's built-in Error class, but supports a "cause" argument which
99 * is wrapped, not "folded in" as with VError. Accepts a printf-style message.
100 * The cause argument can be null.
102 function WError(options)
106 var args, cause, ctor;
107 if (typeof (options) === 'object') {
108 args = Array.prototype.slice.call(arguments, 1);
110 args = Array.prototype.slice.call(arguments, 0);
114 if (args.length > 0) {
115 this.message = mod_extsprintf.sprintf.apply(null, args);
121 if (options instanceof Error) {
124 cause = options.cause;
125 ctor = options.constructorOpt;
129 Error.captureStackTrace(this, ctor || this.constructor);
135 mod_util.inherits(WError, Error);
136 WError.prototype.name = 'WError';
139 WError.prototype.toString = function we_toString()
141 var str = (this.hasOwnProperty('name') && this.name ||
142 this.constructor.name || this.constructor.prototype.name);
144 str += ': ' + this.message;
145 if (this.we_cause && this.we_cause.message)
146 str += '; caused by ' + this.we_cause.toString();
151 WError.prototype.cause = function we_cause(c)
153 if (c instanceof Error)
156 return (this.we_cause);