/src/error.js
JavaScript | 51 lines | 32 code | 9 blank | 10 comment | 6 complexity | 256fdfe655aeeabe0eed57ee4e1d209a MD5 | raw file
Possible License(s): BSD-3-Clause
- /**
- * Error handler for requests
- *
- * @method error
- * @param {Object} req Request Object
- * @param {Object} res Response Object
- * @param {Number} status [Optional] HTTP status code
- * @return {Object} TurtleIO instance
- */
- TurtleIO.prototype.error = function ( req, res, status ) {
- var timer = precise().start(),
- method = req.method.toLowerCase(),
- host = req.parsed ? req.parsed.hostname : ALL,
- kdx = -1,
- body, msg;
- if ( isNaN( status ) ) {
- status = this.codes.NOT_FOUND;
- // If valid, determine what kind of error to respond with
- if ( !REGEX_GET.test( method ) && !REGEX_HEAD.test( method ) ) {
- if ( this.allowed( method, req.url, host ) ) {
- status = this.codes.SERVER_ERROR;
- }
- else {
- status = this.codes.NOT_ALLOWED;
- }
- }
- }
- body = this.page( status, host );
- array.each( array.cast( this.codes ), function ( i, idx ) {
- if ( i === status ) {
- kdx = idx;
- return false;
- }
- } );
- msg = kdx ? array.cast( this.messages )[kdx] : "Unknown error";
- this.log( new Error( "[client " + ( req.headers["x-forwarded-for"] ? array.last( string.explode( req.headers["x-forwarded-for"] ) ) : req.connection.remoteAddress ) + "] " + msg ), "debug" );
- timer.stop();
- this.dtp.fire( "error", function () {
- return [req.headers.host, req.parsed.path, status, msg, timer.diff()];
- } );
- return this.respond( req, res, body, status, {"cache-control": "no-cache", "content-length": Buffer.byteLength( body )} );
- };