How to use http-errors

Comprehensive http-errors code examples:

How to use http-errors.apply:

19
20
21
22
23
24
25
26
27
28
var createError = require('http-errors');
var delegate = require('delegates');
var assert = require('assert');
var proto = module.exports = {
  throw: function() {
    throw createError.apply(null, arguments);
  },
  onerror: function(err) {
    if (null == err)
      return;

How to use http-errors.InternalError:

621
622
623
624
625
626
627
628
629
630
    throw new createHTTPError.BadRequest(err.message);
  } else {
    // an unknown error happened
    // log it and throw internal error
    logger.error(err.stack || err.toString());
    throw new createHTTPError.InternalError('internal error');
  }
}

// return in common format

How to use http-errors.TooEarly:

107
108
109
110
111
112
113
114
115
116
failedDependency: function failedDependency (message) {
  return new createError.FailedDependency(message)
},

tooEarly: function tooEarly (message) {
  return new createError.TooEarly(message)
},

upgradeRequired: function upgradeRequired (message) {
  return new createError.UpgradeRequired(message)

How to use http-errors.ServerInternalError:

99
100
101
102
103
104
105
106
107
108

    /** update blog */
    const updatedBlog = await blogModel.updateOne({'_id': blogId}, {$set: req.body});

    /** throw error if update was unsuccessful */
    if (updatedBlog.updateCount <= 0) throw createError.ServerInternalError("بروزرسانی با مشکل مواجه شد، لطفا مجددا تلاش نمایید");

    this.sendSuccessResponse(req, res, 200, "بروزرسانی با موفقیت انجام شد");
} catch (err) {
    /** remove uploaded file */

How to use http-errors.isHttpError:

83
84
85
86
87
88
89
90
91
92

- `status` - the status code as a number
- `error` - the error object to extend
- `properties` - custom properties to attach to the object

### createError.isHttpError(val)

Determine if the provided `val` is an `HttpError`. This will return `true`
if the error inherits from the `HttpError` constructor of this module or
matches the "duck type" for an error this module creates. All outputs from

How to use http-errors.HttpError:

188
189
190
191
192
193
194
  return httpErrors[statusCodesMap[code + '']](message)
}

module.exports = httpErrors
module.exports.getHttpError = getHttpError
module.exports.HttpError = createError.HttpError
module.exports.createError = createError

How to use http-errors.UnorderedCollection:

107
108
109
110
111
112
113
114
115
116
failedDependency: function failedDependency (message) {
  return new createError.FailedDependency(message)
},

unorderedCollection: function unorderedCollection (message) {
  return new createError.UnorderedCollection(message)
},

upgradeRequired: function upgradeRequired (message) {
  return new createError.UpgradeRequired(message)

How to use http-errors.PaymentRequired:

23
24
25
26
27
28
29
30
31
32
unauthorized: function unauthorized (message) {
  return new createError.Unauthorized(message)
},

paymentRequired: function paymentRequired (message) {
  return new createError.PaymentRequired(message)
},

forbidden: function forbidden (message) {
  return new createError.Forbidden(message)

How to use http-errors.PreconditionRequired:

115
116
117
118
119
120
121
122
123
124
upgradeRequired: function upgradeRequired (message) {
  return new createError.UpgradeRequired(message)
},

preconditionRequired: function preconditionRequired (message) {
  return new createError.PreconditionRequired(message)
},

tooManyRequests: function tooManyRequests (message) {
  return new createError.TooManyRequests(message)

How to use http-errors.RangeNotSatisfiable:

79
80
81
82
83
84
85
86
87
88
unsupportedMediaType: function unsupportedMediaType (message) {
  return new createError.UnsupportedMediaType(message)
},

rangeNotSatisfiable: function rangeNotSatisfiable (message) {
  return new createError.RangeNotSatisfiable(message)
},

expectationFailed: function expectationFailed (message) {
  return new createError.ExpectationFailed(message)

How to use http-errors.VariantAlsoNegotiates:

158
159
160
161
162
163
164
165
166
167
httpVersionNotSupported: function httpVersionNotSupported (message) {
  return new createError.HTTPVersionNotSupported(message)
},

variantAlsoNegotiates: function variantAlsoNegotiates (message) {
  return new createError.VariantAlsoNegotiates(message)
},

insufficientStorage: function insufficientStorage (message) {
  return new createError.InsufficientStorage(message)

How to use http-errors.UpgradeRequired:

111
112
113
114
115
116
117
118
119
120
tooEarly: function tooEarly (message) {
  return new createError.TooEarly(message)
},

upgradeRequired: function upgradeRequired (message) {
  return new createError.UpgradeRequired(message)
},

preconditionRequired: function preconditionRequired (message) {
  return new createError.PreconditionRequired(message)

How to use http-errors.MisdirectedRequest:

91
92
93
94
95
96
97
98
99
100
imateapot: function imateapot (message) {
  return new createError.ImATeapot(message)
},

misdirectedRequest: function misdirectedRequest (message) {
  return new createError.MisdirectedRequest(message)
},

unprocessableEntity: function unprocessableEntity (message) {
  return new createError.UnprocessableEntity(message)

How to use http-errors.RequestHeaderFieldsTooLarge:

123
124
125
126
127
128
129
130
131
132
tooManyRequests: function tooManyRequests (message) {
  return new createError.TooManyRequests(message)
},

requestHeaderFieldsTooLarge: function requestHeaderFieldsTooLarge (message) {
  return new createError.RequestHeaderFieldsTooLarge(message)
},

unavailableForLegalReasons: function unavailableForLegalReasons (message) {
  return new createError.UnavailableForLegalReasons(message)

How to use http-errors.BandwidthLimitExceeded:

170
171
172
173
174
175
176
177
178
179
loopDetected: function loopDetected (message) {
  return new createError.LoopDetected(message)
},

bandwidthLimitExceeded: function bandwidthLimitExceeded (message) {
  return new createError.BandwidthLimitExceeded(message)
},

notExtended: function notExtended (message) {
  return new createError.NotExtended(message)

How to use http-errors.LengthRequired:

59
60
61
62
63
64
65
66
67
68
gone: function gone (message) {
  return new createError.Gone(message)
},

lengthRequired: function lengthRequired (message) {
  return new createError.LengthRequired(message)
},

preconditionFailed: function preconditionFailed (message) {
  return new createError.PreconditionFailed(message)

How to use http-errors.InsufficientStorage:

162
163
164
165
166
167
168
169
170
171
variantAlsoNegotiates: function variantAlsoNegotiates (message) {
  return new createError.VariantAlsoNegotiates(message)
},

insufficientStorage: function insufficientStorage (message) {
  return new createError.InsufficientStorage(message)
},

loopDetected: function loopDetected (message) {
  return new createError.LoopDetected(message)

How to use http-errors.ProxyAuthenticationRequired:

43
44
45
46
47
48
49
50
51
52
notAcceptable: function notAcceptable (message) {
  return new createError.NotAcceptable(message)
},

proxyAuthenticationRequired: function proxyAuthenticationRequired (message) {
  return new createError.ProxyAuthenticationRequired(message)
},

requestTimeout: function requestTimeout (message) {
  return new createError.RequestTimeout(message)

How to use http-errors.Locked:

99
100
101
102
103
104
105
106
107
108
unprocessableEntity: function unprocessableEntity (message) {
  return new createError.UnprocessableEntity(message)
},

locked: function locked (message) {
  return new createError.Locked(message)
},

failedDependency: function failedDependency (message) {
  return new createError.FailedDependency(message)

How to use http-errors.URITooLong:

71
72
73
74
75
76
77
78
79
80
payloadTooLarge: function payloadTooLarge (message) {
  return new createError.PayloadTooLarge(message)
},

uriTooLong: function uriTooLong (message) {
  return new createError.URITooLong(message)
},

unsupportedMediaType: function unsupportedMediaType (message) {
  return new createError.UnsupportedMediaType(message)

How to use http-errors.UnavailableForLegalReasons:

127
128
129
130
131
132
133
134
135
136
requestHeaderFieldsTooLarge: function requestHeaderFieldsTooLarge (message) {
  return new createError.RequestHeaderFieldsTooLarge(message)
},

unavailableForLegalReasons: function unavailableForLegalReasons (message) {
  return new createError.UnavailableForLegalReasons(message)
},

internalServerError: function internalServerError (message) {
  const error = new createError.InternalServerError(message)

How to use http-errors.NetworkAuthenticationRequired:

178
179
180
181
182
183
184
185
186
187
  notExtended: function notExtended (message) {
    return new createError.NotExtended(message)
  },

  networkAuthenticationRequired: function networkAuthenticationRequired (message) {
    return new createError.NetworkAuthenticationRequired(message)
  }
}

function getHttpError (code, message) {

How to use http-errors.HTTPVersionNotSupported:

154
155
156
157
158
159
160
161
162
163
gatewayTimeout: function gatewayTimeout (message) {
  return new createError.GatewayTimeout(message)
},

httpVersionNotSupported: function httpVersionNotSupported (message) {
  return new createError.HTTPVersionNotSupported(message)
},

variantAlsoNegotiates: function variantAlsoNegotiates (message) {
  return new createError.VariantAlsoNegotiates(message)

How to use http-errors.ImATeapot:

87
88
89
90
91
92
93
94
95
96
expectationFailed: function expectationFailed (message) {
  return new createError.ExpectationFailed(message)
},

imateapot: function imateapot (message) {
  return new createError.ImATeapot(message)
},

misdirectedRequest: function misdirectedRequest (message) {
  return new createError.MisdirectedRequest(message)

How to use http-errors.FailedDependency:

103
104
105
106
107
108
109
110
111
112
locked: function locked (message) {
  return new createError.Locked(message)
},

failedDependency: function failedDependency (message) {
  return new createError.FailedDependency(message)
},

tooEarly: function tooEarly (message) {
  return new createError.TooEarly(message)

How to use http-errors.LoopDetected:

166
167
168
169
170
171
172
173
174
175
insufficientStorage: function insufficientStorage (message) {
  return new createError.InsufficientStorage(message)
},

loopDetected: function loopDetected (message) {
  return new createError.LoopDetected(message)
},

bandwidthLimitExceeded: function bandwidthLimitExceeded (message) {
  return new createError.BandwidthLimitExceeded(message)

How to use http-errors.Gone:

55
56
57
58
59
60
61
62
63
64
conflict: function conflict (message) {
  return new createError.Conflict(message)
},

gone: function gone (message) {
  return new createError.Gone(message)
},

lengthRequired: function lengthRequired (message) {
  return new createError.LengthRequired(message)

How to use http-errors.UnsupportedMediaType:

75
76
77
78
79
80
81
82
83
84
uriTooLong: function uriTooLong (message) {
  return new createError.URITooLong(message)
},

unsupportedMediaType: function unsupportedMediaType (message) {
  return new createError.UnsupportedMediaType(message)
},

rangeNotSatisfiable: function rangeNotSatisfiable (message) {
  return new createError.RangeNotSatisfiable(message)

How to use http-errors.GatewayTimeout:

150
151
152
153
154
155
156
157
158
159
serviceUnavailable: function serviceUnavailable (message) {
  return new createError.ServiceUnavailable(message)
},

gatewayTimeout: function gatewayTimeout (message) {
  return new createError.GatewayTimeout(message)
},

httpVersionNotSupported: function httpVersionNotSupported (message) {
  return new createError.HTTPVersionNotSupported(message)

How to use http-errors.BadGateway:

209
210
211
212
213
214
215
216
217
218
    `proto=${socket.encrypted ? 'https' : 'http'}`,
    `host=${printIp(authority || host || '')}`
  ].join(';'))
} else if (forwarded) {
  // The forwarded header should not be included in response.
  throw new createError.BadGateway()
}

if (proxyName) {
  if (via) {