"Fossies" - the Fresh Open Source Software Archive

Member "Atom/resources/app/apm/node_modules/boom/lib/index.js" (8 Mar 2017, 7650 Bytes) of archive /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 // Load modules
    2 
    3 var Http = require('http');
    4 var Hoek = require('hoek');
    5 
    6 
    7 // Declare internals
    8 
    9 var internals = {};
   10 
   11 exports.wrap = function (error, statusCode, message) {
   12 
   13     Hoek.assert(error instanceof Error, 'Cannot wrap non-Error object');
   14     return (error.isBoom ? error : internals.initialize(error, statusCode || 500, message));
   15 };
   16 
   17 
   18 exports.create = function (statusCode, message, data) {
   19 
   20     return internals.create(statusCode, message, data, exports.create);
   21 };
   22 
   23 internals.create = function (statusCode, message, data, ctor) {
   24 
   25     var error = new Error(message ? message : undefined);       // Avoids settings null message
   26     Error.captureStackTrace(error, ctor);                       // Filter the stack to our external API
   27     error.data = data || null;
   28     internals.initialize(error, statusCode);
   29     return error;
   30 };
   31 
   32 internals.initialize = function (error, statusCode, message) {
   33 
   34     var numberCode = parseInt(statusCode, 10);
   35     Hoek.assert(!isNaN(numberCode) && numberCode >= 400, 'First argument must be a number (400+):', statusCode);
   36 
   37     error.isBoom = true;
   38     error.isServer = numberCode >= 500;
   39 
   40     if (!error.hasOwnProperty('data')) {
   41         error.data = null;
   42     }
   43 
   44     error.output = {
   45         statusCode: numberCode,
   46         payload: {},
   47         headers: {}
   48     };
   49 
   50     error.reformat = internals.reformat;
   51     error.reformat();
   52 
   53     if (!message &&
   54         !error.message) {
   55 
   56         message = error.output.payload.error;
   57     }
   58 
   59     if (message) {
   60         error.message = (message + (error.message ? ': ' + error.message : ''));
   61     }
   62 
   63     return error;
   64 };
   65 
   66 
   67 internals.reformat = function () {
   68 
   69     this.output.payload.statusCode = this.output.statusCode;
   70     this.output.payload.error = Http.STATUS_CODES[this.output.statusCode] || 'Unknown';
   71 
   72     if (this.output.statusCode === 500) {
   73         this.output.payload.message = 'An internal server error occurred';              // Hide actual error from user
   74     }
   75     else if (this.message) {
   76         this.output.payload.message = this.message;
   77     }
   78 };
   79 
   80 
   81 // 4xx Client Errors
   82 
   83 exports.badRequest = function (message, data) {
   84 
   85     return internals.create(400, message, data, exports.badRequest);
   86 };
   87 
   88 
   89 exports.unauthorized = function (message, scheme, attributes) {          // Or function (message, wwwAuthenticate[])
   90 
   91     var err = internals.create(401, message, undefined, exports.unauthorized);
   92 
   93     if (!scheme) {
   94         return err;
   95     }
   96 
   97     var wwwAuthenticate = '';
   98     var i = 0;
   99     var il = 0;
  100 
  101     if (typeof scheme === 'string') {
  102 
  103         // function (message, scheme, attributes)
  104 
  105         wwwAuthenticate = scheme;
  106 
  107         if (attributes || message) {
  108             err.output.payload.attributes = {};
  109         }
  110 
  111         if (attributes) {
  112             var names = Object.keys(attributes);
  113             for (i = 0, il = names.length; i < il; ++i) {
  114                 var name = names[i];
  115                 if (i) {
  116                     wwwAuthenticate += ',';
  117                 }
  118 
  119                 var value = attributes[name];
  120                 if (value === null ||
  121                     value === undefined) {              // Value can be zero
  122 
  123                     value = '';
  124                 }
  125                 wwwAuthenticate += ' ' + name + '="' + Hoek.escapeHeaderAttribute(value.toString()) + '"';
  126                 err.output.payload.attributes[name] = value;
  127             }
  128         }
  129 
  130         if (message) {
  131             if (attributes) {
  132                 wwwAuthenticate += ',';
  133             }
  134             wwwAuthenticate += ' error="' + Hoek.escapeHeaderAttribute(message) + '"';
  135             err.output.payload.attributes.error = message;
  136         }
  137         else {
  138             err.isMissing = true;
  139         }
  140     }
  141     else {
  142 
  143         // function (message, wwwAuthenticate[])
  144 
  145         var wwwArray = scheme;
  146         for (i = 0, il = wwwArray.length; i < il; ++i) {
  147             if (i) {
  148                 wwwAuthenticate += ', ';
  149             }
  150 
  151             wwwAuthenticate += wwwArray[i];
  152         }
  153     }
  154 
  155     err.output.headers['WWW-Authenticate'] = wwwAuthenticate;
  156 
  157     return err;
  158 };
  159 
  160 
  161 exports.forbidden = function (message, data) {
  162 
  163     return internals.create(403, message, data, exports.forbidden);
  164 };
  165 
  166 
  167 exports.notFound = function (message, data) {
  168 
  169     return internals.create(404, message, data, exports.notFound);
  170 };
  171 
  172 
  173 exports.methodNotAllowed = function (message, data) {
  174 
  175     return internals.create(405, message, data, exports.methodNotAllowed);
  176 };
  177 
  178 
  179 exports.notAcceptable = function (message, data) {
  180 
  181     return internals.create(406, message, data, exports.notAcceptable);
  182 };
  183 
  184 
  185 exports.proxyAuthRequired = function (message, data) {
  186 
  187     return internals.create(407, message, data, exports.proxyAuthRequired);
  188 };
  189 
  190 
  191 exports.clientTimeout = function (message, data) {
  192 
  193     return internals.create(408, message, data, exports.clientTimeout);
  194 };
  195 
  196 
  197 exports.conflict = function (message, data) {
  198 
  199     return internals.create(409, message, data, exports.conflict);
  200 };
  201 
  202 
  203 exports.resourceGone = function (message, data) {
  204 
  205     return internals.create(410, message, data, exports.resourceGone);
  206 };
  207 
  208 
  209 exports.lengthRequired = function (message, data) {
  210 
  211     return internals.create(411, message, data, exports.lengthRequired);
  212 };
  213 
  214 
  215 exports.preconditionFailed = function (message, data) {
  216 
  217     return internals.create(412, message, data, exports.preconditionFailed);
  218 };
  219 
  220 
  221 exports.entityTooLarge = function (message, data) {
  222 
  223     return internals.create(413, message, data, exports.entityTooLarge);
  224 };
  225 
  226 
  227 exports.uriTooLong = function (message, data) {
  228 
  229     return internals.create(414, message, data, exports.uriTooLong);
  230 };
  231 
  232 
  233 exports.unsupportedMediaType = function (message, data) {
  234 
  235     return internals.create(415, message, data, exports.unsupportedMediaType);
  236 };
  237 
  238 
  239 exports.rangeNotSatisfiable = function (message, data) {
  240 
  241     return internals.create(416, message, data, exports.rangeNotSatisfiable);
  242 };
  243 
  244 
  245 exports.expectationFailed = function (message, data) {
  246 
  247     return internals.create(417, message, data, exports.expectationFailed);
  248 };
  249 
  250 exports.badData = function (message, data) {
  251 
  252     return internals.create(422, message, data, exports.badData);
  253 };
  254 
  255 
  256 exports.preconditionRequired = function (message, data) {
  257 
  258     return internals.create(428, message, data, exports.preconditionRequired);
  259 };
  260 
  261 
  262 exports.tooManyRequests = function (message, data) {
  263 
  264     return internals.create(429, message, data, exports.tooManyRequests);
  265 };
  266 
  267 
  268 // 5xx Server Errors
  269 
  270 exports.internal = function (message, data, statusCode) {
  271 
  272     return internals.serverError(message, data, statusCode, exports.internal);
  273 };
  274 
  275 internals.serverError = function (message, data, statusCode, ctor) {
  276 
  277     var error;
  278     if (data instanceof Error) {
  279         error = exports.wrap(data, statusCode, message);
  280     } else {
  281         error = internals.create(statusCode || 500, message, undefined, ctor);
  282         error.data = data;
  283     }
  284 
  285     return error;
  286 };
  287 
  288 
  289 exports.notImplemented = function (message, data) {
  290 
  291     return internals.serverError(message, data, 501, exports.notImplemented);
  292 };
  293 
  294 
  295 exports.badGateway = function (message, data) {
  296 
  297     return internals.serverError(message, data, 502, exports.badGateway);
  298 };
  299 
  300 
  301 exports.serverTimeout = function (message, data) {
  302 
  303     return internals.serverError(message, data, 503, exports.serverTimeout);
  304 };
  305 
  306 
  307 exports.gatewayTimeout = function (message, data) {
  308 
  309     return internals.serverError(message, data, 504, exports.gatewayTimeout);
  310 };
  311 
  312 
  313 exports.badImplementation = function (message, data) {
  314 
  315     var err = internals.serverError(message, data, 500, exports.badImplementation);
  316     err.isDeveloperError = true;
  317     return err;
  318 };