You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
41 lines
1.0 KiB
41 lines
1.0 KiB
10 years ago
|
'use strict';
|
||
|
|
||
|
module.exports = Response;
|
||
|
|
||
|
/**
|
||
|
* A response from a web request
|
||
|
*
|
||
|
* @param {Number} statusCode
|
||
|
* @param {Object} headers
|
||
|
* @param {Buffer} body
|
||
|
*/
|
||
|
function Response(statusCode, headers, body) {
|
||
|
if (typeof statusCode !== 'number') {
|
||
|
throw new TypeError('statusCode must be a number but was ' + (typeof statusCode));
|
||
|
}
|
||
|
if (headers === null) {
|
||
|
throw new TypeError('headers cannot be null');
|
||
|
}
|
||
|
if (typeof headers !== 'object') {
|
||
|
throw new TypeError('headers must be an object but was ' + (typeof headers));
|
||
|
}
|
||
|
this.statusCode = statusCode;
|
||
|
this.headers = {};
|
||
|
for (var key in headers) {
|
||
|
this.headers[key.toLowerCase()] = headers[key];
|
||
|
}
|
||
|
this.body = body;
|
||
|
}
|
||
|
|
||
|
Response.prototype.getBody = function () {
|
||
|
if (this.statusCode >= 300) {
|
||
|
var err = new Error('Server responded with status code '
|
||
|
+ this.statusCode + ':\n' + this.body.toString());
|
||
|
err.statusCode = this.statusCode;
|
||
|
err.headers = this.headers;
|
||
|
err.body = this.body;
|
||
|
throw err;
|
||
|
}
|
||
|
return this.body;
|
||
|
};
|