wzp
2021-05-13 7d694a9113118daec5be7ac224dab46a3b20f106
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// Copyright 2012 Mark Cavage, Inc.  All rights reserved.
 
'use strict';
 
// core modules
var http = require('http');
var util = require('util');
 
// external modules
var _ = require('lodash');
 
// local files
var helpers = require('./helpers');
var HttpError = require('./baseClasses/HttpError');
 
 
// Programatically create 4xx and 5xx HTTP status codes Error classes
// This list includes:
// BadRequestError
// UnauthorizedError
// PaymentRequiredError
// ForbiddenError
// NotFoundError
// MethodNotAllowedError
// NotAcceptableError
// ProxyAuthenticationRequiredError
// RequestTimeoutError
// ConflictError
// GoneError
// LengthRequiredError
// PreconditionFailedError
// RequestEntityTooLargeError
// RequesturiTooLargeError
// UnsupportedMediaTypeError
// RangeNotSatisfiableError (For Node >= 4 & iojs >= 3)
// RequestedRangeNotSatisfiableError (For Node 0.x & iojs < 3)
// ExpectationFailedError
// ImATeapotError
// UnprocessableEntityError
// LockedError
// FailedDependencyError
// UnorderedCollectionError
// UpgradeRequiredError
// PreconditionRequiredError
// TooManyRequestsError
// RequestHeaderFieldsTooLargeError
// InternalServerError
// NotImplementedError
// BadGatewayError
// ServiceUnavailableError
// GatewayTimeoutError
// HttpVersionNotSupportedError
// VariantAlsoNegotiatesError
// InsufficientStorageError
// BandwidthLimitExceededError
// NotExtendedError
// NetworkAuthenticationRequiredError
var httpErrors = _.reduce(http.STATUS_CODES, function(acc, desc, code) {
 
    var parsedCode = parseInt(code, 10);
 
    if (parsedCode >= 400) {
        var name = helpers.errNameFromDesc(desc);
 
        // this is a dynamic constructor for an error message.
        // arguments are variadic. constructor fn name must be anonymous.
        /**
         * Variadic signature, first two are special to Restify, using a
         * options obj.
         * 1) new [Dynamic]Error(anotherErr, {...});
         * 2) new [Dynamic]Error({...});
         * Last one is a straight pass through to WError
         * 3) new [Dynamic]Error('my special error message');
         * @public
         * @class
         */
        acc[name] = function() {
            // call super
            HttpError.apply(this, arguments);
        };
        util.inherits(acc[name], HttpError);
 
        /**
         * assign non-standard display name property on the CONSTRUCTOR (not
         * prototype), which is supported by all VMs. useful for stack trace
         * output.
         * @type {String}
         */
        acc[name].displayName = name;
 
        /**
         * the name of the error, used in the stack trace output
         * @type {String}
         */
        acc[name].prototype.name = name;
 
        /**
         * assign a default status code based on core http module.
         * users can override this if they want to. HttpError constructor
         * will handle overriding at the instance level.
         * @type {Number}
         */
        acc[name].prototype.statusCode = parsedCode;
 
        /**
         * default code is the error name
         * @type {String}
         */
        acc[name].prototype.code = name.replace(new RegExp('Error$'), '');
    }
 
    return acc;
}, {});
 
 
 
module.exports = httpErrors;