"Fossies" - the Fresh Open Source Software Archive

Member "Atom/resources/app/apm/node_modules/verror/lib/verror.js" (11 Apr 2017, 3589 Bytes) of package /windows/misc/atom-windows.zip:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Javascript source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 /*
    2  * verror.js: richer JavaScript errors
    3  */
    4 
    5 var mod_assert = require('assert');
    6 var mod_util = require('util');
    7 
    8 var mod_extsprintf = require('extsprintf');
    9 
   10 /*
   11  * Public interface
   12  */
   13 exports.VError = VError;
   14 exports.WError = WError;
   15 exports.MultiError = MultiError;
   16 
   17 /*
   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.
   20  */
   21 function VError(options)
   22 {
   23     var args, causedBy, ctor, tailmsg;
   24 
   25     if (options instanceof Error || typeof (options) === 'object') {
   26         args = Array.prototype.slice.call(arguments, 1);
   27     } else {
   28         args = Array.prototype.slice.call(arguments, 0);
   29         options = undefined;
   30     }
   31 
   32     tailmsg = args.length > 0 ?
   33         mod_extsprintf.sprintf.apply(null, args) : '';
   34     this.jse_shortmsg = tailmsg;
   35     this.jse_summary = tailmsg;
   36 
   37     if (options) {
   38         causedBy = options.cause;
   39 
   40         if (!causedBy || !(options.cause instanceof Error))
   41             causedBy = options;
   42 
   43         if (causedBy && (causedBy instanceof Error)) {
   44             this.jse_cause = causedBy;
   45             this.jse_summary += ': ' + causedBy.message;
   46         }
   47     }
   48 
   49     this.message = this.jse_summary;
   50     Error.call(this, this.jse_summary);
   51 
   52     if (Error.captureStackTrace) {
   53         ctor = options ? options.constructorOpt : undefined;
   54         ctor = ctor || arguments.callee;
   55         Error.captureStackTrace(this, ctor);
   56     }
   57 }
   58 
   59 mod_util.inherits(VError, Error);
   60 VError.prototype.name = 'VError';
   61 
   62 VError.prototype.toString = function ve_toString()
   63 {
   64     var str = (this.hasOwnProperty('name') && this.name ||
   65         this.constructor.name || this.constructor.prototype.name);
   66     if (this.message)
   67         str += ': ' + this.message;
   68 
   69     return (str);
   70 };
   71 
   72 VError.prototype.cause = function ve_cause()
   73 {
   74     return (this.jse_cause);
   75 };
   76 
   77 
   78 /*
   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.
   83  */
   84 function MultiError(errors)
   85 {
   86     mod_assert.ok(errors.length > 0);
   87     this.ase_errors = errors;
   88 
   89     VError.call(this, errors[0], 'first of %d error%s',
   90         errors.length, errors.length == 1 ? '' : 's');
   91 }
   92 
   93 mod_util.inherits(MultiError, VError);
   94 
   95 
   96 
   97 /*
   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.
  101  */
  102 function WError(options)
  103 {
  104     Error.call(this);
  105 
  106     var args, cause, ctor;
  107     if (typeof (options) === 'object') {
  108         args = Array.prototype.slice.call(arguments, 1);
  109     } else {
  110         args = Array.prototype.slice.call(arguments, 0);
  111         options = undefined;
  112     }
  113 
  114     if (args.length > 0) {
  115         this.message = mod_extsprintf.sprintf.apply(null, args);
  116     } else {
  117         this.message = '';
  118     }
  119 
  120     if (options) {
  121         if (options instanceof Error) {
  122             cause = options;
  123         } else {
  124             cause = options.cause;
  125             ctor = options.constructorOpt;
  126         }
  127     }
  128 
  129     Error.captureStackTrace(this, ctor || this.constructor);
  130     if (cause)
  131         this.cause(cause);
  132 
  133 }
  134 
  135 mod_util.inherits(WError, Error);
  136 WError.prototype.name = 'WError';
  137 
  138 
  139 WError.prototype.toString = function we_toString()
  140 {
  141     var str = (this.hasOwnProperty('name') && this.name ||
  142         this.constructor.name || this.constructor.prototype.name);
  143     if (this.message)
  144         str += ': ' + this.message;
  145     if (this.we_cause && this.we_cause.message)
  146         str += '; caused by ' + this.we_cause.toString();
  147 
  148     return (str);
  149 };
  150 
  151 WError.prototype.cause = function we_cause(c)
  152 {
  153     if (c instanceof Error)
  154         this.we_cause = c;
  155 
  156     return (this.we_cause);
  157 };