lib/middleware.js
/**
 * Middleware that handles various types of errors, especially the ones that
 * Twitter returns. It returns a request-response handler that sets headers and
 * status codes, and renders an error page.
 *
 * @todo handle Dropbox errors
 * (error.status, error.error.error_summary)
 *
 * @param  {Function} handler
 * @return {Function}
 */
Middleware that handles various types of errors, especially the ones that
Twitter returns. It returns a request-response handler that sets headers and
status codes, and renders an error page.



@param  {Function} handler
@return {Function}
Middleware that handles various types of errors especially the ones that Twitter returns . It returns a request - response handler that sets headers and status codes and renders an error page . @param { Function } handler @return { Function }
function handleErrors(handler) {
	if (typeof handler !== 'function') {
		throw Error('Error handling middleware expects a function')
	}
	return async (req, res) => {
		try {
			await handler(req, res)
		} catch(error) {
			log(error)
			const rateLimit = error._headers ? rateLimitInfo(error._headers) : false
			const message = getErrorMessage(error, rateLimit)
			const statusCode = getStatusCode(error)
			if (statusCode === TOO_MANY_REQUESTS) {
				res.setHeader('Retry-After', rateLimit.secondsToReset)
			}
			res.status(statusCode)
			res.send(errorPage(message))
		}
	}
}
function handleErrors ( handler ) { if ( typeof handler !== 'function' ) { throw Error ( 'Error handling middleware expects a function' ) } return async ( req , res ) => { try { await handler ( req , res ) } catch ( error ) { log ( error ) const rateLimit = error . _headers ? rateLimitInfo ( error . _headers ) : false const message = getErrorMessage ( error , rateLimit ) const statusCode = getStatusCode ( error ) if ( statusCode === TOO_MANY_REQUESTS ) { res . setHeader ( 'Retry-After' , rateLimit . secondsToReset ) } res . status ( statusCode ) res . send ( errorPage ( message ) ) } } }
Middleware that handles various types of errors, especially the ones that
 Twitter returns.
Middleware that handles various types of errors especially the ones that Twitter returns .