-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.js
More file actions
71 lines (60 loc) · 2.06 KB
/
index.js
File metadata and controls
71 lines (60 loc) · 2.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/*!
* HTTPError
* Copyright(c) 2013 Rémy Loubradou
* Author Rémy Loubradou <remyloubradou@gmail.com>
* 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){
// N.B: Unfortunately the ES5 spec specifies that Error.call(this)
// must always return a new object, and therefore it cannot be used for inheritance
// See more section 15.11.1 of ECMA-262 5.1 Edition
// http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-262.pdf
// Error.call(this);
// Here in order to capture the stack we use Error.captureStackTrace
// it sets this.stack
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);