<commit_before>/*!
 * HTTPError
 * Copyright(c) 2013 <NAME>
 * Author <NAME> <<EMAIL>>
 * MIT Licensed
 */

/**
 * Module dependencies
 */

var http = require('http'),
	util = require('util'),
	STATUS_CODES = http.STATUS_CODES;

/**
 * Expose `HTTPError` class
 */

module.exports = HTTPError;


/**
 * Initialize a `HTTPError` object
 *
 * @param {Number|String} code - HTTP status code
 *
 * @constructor
 * @extends {Error}
 *
 * @api public
 */

function HTTPError(code){
	Error.call(this);
	Error.captureStackTrace(this,arguments.callee);
	this.name = 'HTTPError';
	var description = STATUS_CODES[code] || 'Unknown status code';
	this.message = code + ' - ' + description;
}

util.inherits(HTTPError,Error);<commit_msg>NEW: Build a more interesting message from the http request and response.<commit_after>/*!
 * HTTPError
 * Copyright(c) 2013 <NAME>
 * Author <NAME> <<EMAIL>>
 * MIT Licensed
 */

/**
 * Module dependencies
 */

var http = require('http'),
	util = require('util'),
	url = require('url'),
	hasNestedProperty = require('hnp'),
	STATUS_CODES = http.STATUS_CODES;

/**
 * Expose `HTTPError` class
 */

module.exports = HTTPError;


/**
 * Initialize a `HTTPError` object
 * @constructor
 *
 * @param {http.ClientRequest}  request -
 * @param {http.IncomingMessage} response -
 * @param {String} [message] - message prepended in the Error#message property,
 *  default to empty string
 * @inherits {Error}
 *
 * @api public
 */

function HTTPError(req,res,message){
	Error.call(this);
	Error.captureStackTrace(this,arguments.callee);
	this.name = 'HTTPError';

	var description = STATUS_CODES[res.statusCode] || 'Unknown status code';
	req.headers = req.headers || req._headers;
	var reqHeaders = Object.keys(req.headers).map(function(name){
		return name + ': ' + req.headers[name]
	});
	var resHeaders = Object.keys(res.headers).map(function(name){
		return name + ': ' + res.headers[name]
	});

	var protocol = 'http';
	if(hasNestedProperty(req,'socket.pair.ssl')) protocol = 'https';
	if(!req.uri) req.uri =  protocol + '://' + req.headers['host'] + req.path;

	this.message = (message || '') + '\r\n'
		+ 'Request URL: ' + url.format(req.uri || {}) + '\r\n'
		+ 'Request method: ' + req.method + '\r\n'
		+ 'Status code: ' + res.statusCode + ' - ' + description + '\r\n'
		+ 'Request headers: \r\n' + reqHeaders.join('\r\n') + '\r\n'
		+ 'Response headers: \r\n' + resHeaders.join('\r\n')
		+ (res.body ? '\r\nResponse body: \r\n' + res.body : '');
}

util.inherits(HTTPError,Error);
{'fraction_non_alphanumeric': 0.18268847227762267, 'fraction_numerical': 0.0031910650179497405, 'mean_word_length': 3.9467455621301775, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5943671', 'n_tokens_mistral': 876, 'n_tokens_neox': 888, 'n_words': 190}