Skip to content

Commit

Permalink
Merge pull request #237 from walmartlabs/user/eran
Browse files Browse the repository at this point in the history
Response refactor
  • Loading branch information
geek committed Nov 15, 2012
2 parents 0f202ce + e29c77f commit d0464f4
Show file tree
Hide file tree
Showing 15 changed files with 742 additions and 450 deletions.
5 changes: 3 additions & 2 deletions examples/stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@ internals.main = function () {

internals.echo = function (request) {

request.reply.type(request.raw.req.headers['Content-Type'])
request.reply.stream(request.raw.req)
.type(request.raw.req.headers['Content-Type'])
.bytes(request.raw.req.headers['Content-Length'])
.stream(request.raw.req);
.send();
};


Expand Down
2 changes: 1 addition & 1 deletion lib/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ exports.server = {
onPostHandler: null, // After route handler returns, before sending response
onPostRoute: null, // After response sent

// function (request) { request.reply.send(result); OR request.reply.close(); }
// function (request) { request.reply(result); OR request.reply.close(); }
onUnknownRoute: null // Overrides hapi's default handler for unknown route. Cannot be an array!
},

Expand Down
91 changes: 37 additions & 54 deletions lib/error.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,31 @@ var Utils = require('./utils');
var internals = {};


exports = module.exports = internals.Error = function (code, message, options) {
exports = module.exports = internals.Error = function (code, message) {

Utils.assert(this.constructor === internals.Error, 'Error must be instantiated using new');
Utils.assert(!options || !options.toResponse || typeof options.toResponse === 'function', 'options.toReponse must be a function');
Utils.assert(code >= 400, 'Error code must be 4xx or 5xx');
Utils.assert(code instanceof Error || (!isNaN(parseFloat(code)) && isFinite(code) && code >= 400), 'code must be an Error or a number (400+)');

Error.call(this);

this.code = code;
this.message = message;
this._settings = Utils.clone(options) || {}; // options can be reused;
if (code instanceof Error) {
for (var d in code) {
if (code.hasOwnProperty(d)) {
this[d] = code[d];
}
}

this.code = 500;
this.name = code.name;
this.message = code.message || message;
if (code.message && message) {
this.info = message;
}
}
else {
this.code = code;
this.message = message;
}

return this;
};
Expand All @@ -30,23 +44,19 @@ NodeUtil.inherits(internals.Error, Error);

internals.Error.prototype.toResponse = function () {

if (this._settings.toResponse) {
return this._settings.toResponse.call(this);
}

var response = {
code: this.code,
payload: {
error: Http.STATUS_CODES[this.code] || 'Unknown',
code: this.code,
message: this.message
}
// contentType: 'application/json'
};

for (var d in this) {
if (this.hasOwnProperty(d) &&
!response.payload.hasOwnProperty(d)) {
!response.payload.hasOwnProperty(d) &&
typeof this[d] !== 'function') {

response.payload[d] = this[d];
}
Expand Down Expand Up @@ -84,7 +94,11 @@ internals.Error.notFound = function (message) {

internals.Error.internal = function (message, data) {

var format = function () {
var err = new internals.Error(500, message);
err.trace = Utils.callStack(1);
err.data = data;

err.toResponse = function () {

var response = {
code: 500,
Expand All @@ -98,63 +112,32 @@ internals.Error.internal = function (message, data) {
return response;
};

var err = new internals.Error(500, message, { toResponse: format });
err.trace = Utils.callStack(1);
err.data = data;
return err;
};


internals.Error.passThrough = function (code, payload, contentType) {

var format = function () {
var err = new internals.Error(500, 'Pass-through'); // 500 code is only used internally and is not exposed when sent

err.passThrough = {
code: code,
payload: payload,
type: contentType
};

err.toResponse = function () {

var response = {
code: code,
payload: payload,
contentType: contentType
type: contentType
};

return response;
};

var err = new internals.Error(500, 'Pass-through', { toResponse: format }); // 500 code is only used internally and is not exposed when sent

err.passThrough = {
code: code,
payload: payload,
contentType: contentType
};

return err;
};


internals.Error.toResponse = function (err) {

Utils.assert(err instanceof Error, 'Input must be instance of Error');

if (err.toResponse &&
typeof err.toResponse === 'function') {

return err.toResponse();
}

// Other Error

var response = {
code: 500,
payload: {
message: err.message,
name: err.name
}
};

for (var d in err) {
if (err.hasOwnProperty(d)) {
response.payload[d] = err[d];
}
}

return response;
};
1 change: 1 addition & 0 deletions lib/hapi.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ var internals = {
error: require('./error'),
log: require('./log'),
server: require('./server'),
response: require('./response'),
utils: require('./utils'),
types: require('joi').Types
}
Expand Down
7 changes: 4 additions & 3 deletions lib/proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ internals.Proxy.prototype.handler = function () {

reqStream.on('response', function (resStream) {

request.reply.stream(resStream); // Request._respond will pass-through headers and status code
request.reply(resStream); // Request._respond will pass-through headers and status code
});
}
});
Expand Down Expand Up @@ -134,9 +134,10 @@ internals.postResponse = function (request, settings, response, payload) {
return request.reply(Err.passThrough(statusCode, payload, contentType));
}

var response = request.reply.payload(payload);
if (contentType) {
request.reply.type(contentType);
response.type(contentType);
}

return request.reply(payload);
return response.send();
};
Loading

0 comments on commit d0464f4

Please sign in to comment.