MCPcopy Index your code
hub / github.com/restify/node-restify / call

Function call

lib/chain.js:174–234  ·  view source on GitHub ↗

* Invoke a handler. * * @private * @param {Function} handler - handler function * @param {Error|false|*} err - error, abort when true value or false * @param {Request} req - request * @param {Response} res - response * @param {Function} _next - next handler * @returns {undefined} no return v

(handler, err, req, res, _next)

Source from the content-addressed store, hash-verified

172 * @returns {undefined} no return value
173 */
174function call(handler, err, req, res, _next) {
175 var arity = handler.length;
176 var hasError = err === false || Boolean(err);
177
178 // Meassure handler timings
179 // _name is assigned in the server and router
180 req._currentHandler = handler._name;
181 req.startHandlerTimer(handler._name);
182
183 function next(nextErr) {
184 req.endHandlerTimer(handler._name);
185 _next(nextErr, req, res);
186 }
187
188 function resolve(value) {
189 if (value && req.log) {
190 // logs resolved value
191 req.log.warn(
192 { value },
193 'Discarded returned value from async handler'
194 );
195 }
196
197 return next();
198 }
199
200 function reject(error) {
201 if (!(error instanceof Error)) {
202 error = new customErrorTypes.AsyncError(
203 {
204 info: {
205 cause: error,
206 handler: handler._name,
207 method: req.method,
208 path: req.path ? req.path() : undefined
209 }
210 },
211 'Async middleware rejected without an error'
212 );
213 }
214 return next(error);
215 }
216
217 if (hasError && arity === 4) {
218 // error-handling middleware
219 handler(err, req, res, next);
220 return;
221 } else if (!hasError && arity < 4) {
222 // request-handling middleware
223 process.nextTick(function nextTick() {
224 const result = handler(req, res, next);
225 if (result && typeof result.then === 'function') {
226 result.then(resolve, reject);
227 }
228 });
229 return;
230 }
231

Callers 1

nextFunction · 0.85

Calls 2

nextFunction · 0.70
handlerFunction · 0.50

Tested by

no test coverage detected