From b710f1c071d75d4b028c61bb6f3927c2cd4aadf5 Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Wed, 4 Jul 2012 13:34:51 +0200 Subject: o Always showing the http response, even when the body couldn't be decoded. --- routes/index.js | 68 +++++++++++++++++++++++++++++++-------------------------- 1 file changed, 37 insertions(+), 31 deletions(-) (limited to 'routes') diff --git a/routes/index.js b/routes/index.js index fca41dd..e84f15d 100644 --- a/routes/index.js +++ b/routes/index.js @@ -18,10 +18,14 @@ exports.index = function(req, res){ }; exports.render = function(req, res) { - function sendErr(err, rawBody) { + function sendErr(req, err, statusCode, status, headers, rawBody) { res.render('data', { + urlgenerator: urlgenerator(req), url: req.query.url, err: err, + statusCode: statusCode, + status: status, + headers: headers, rawBody: rawBody }); } @@ -34,39 +38,41 @@ exports.render = function(req, res) { return q; }, {}); u.query = _.extend({}, u.query, params); - fetchCollection(url.format(u), function(err, headers, body) { + fetchCollection(url.format(u), function(err, statusCode, status, headers, body) { if(err) { - sendErr(err, body); + sendErr(req, err, statusCode, status, headers, body); + return; } - else { - var parsedBody; + var parsedBody; + try { + parsedBody = JSON.parse(body); + } catch(e) { + sendErr(req, 'Unable to parse JSON: ' + e, statusCode, status, headers, body); + return; + } + var collection = collection_json.fromObject(parsedBody).collection; + var isUrl = function(u) { try { - parsedBody = JSON.parse(body); - } catch(e) { - sendErr('Unable to parse JSON: ' + e, body); - return; + var x = url.parse(u); + return _.isString(x.protocol) && _.isString(x.host) && _.isString(path); } - var collection = collection_json.fromObject(parsedBody).collection; - console.log(collection); - var isUrl = function(u) { - try { - var x = url.parse(u); - return _.isString(x.protocol) && _.isString(x.host) && _.isString(path); - } - catch(e) { - return false; - } - }; - res.render('data', { - isUrl: isUrl, - urlgenerator: urlgenerator(req), - url: req.query.url, - params: params, - collection: collection, - headers: headers, - rawBody: body, - formattedBody: JSON.stringify(parsedBody, null, ' ') }); - } + catch(e) { + return false; + } + }; + res.render('data', { + isUrl: isUrl, + urlgenerator: urlgenerator(req), + url: req.query.url, + params: params, + collection: collection, + statusCode: statusCode, + status: status, + headers: headers, + headers: headers, + rawBody: body, + formattedBody: JSON.stringify(parsedBody, null, ' ') + }); }); }; @@ -82,7 +88,7 @@ function fetchCollection(u, cb) { body += chunk; }); res.on('end', function (chunk) { - cb(undefined, res.headers, body); + cb(undefined, res.stausCode, "", res.headers, body); }); }).on('error', function() { cb('Unable to fetch ' + u); -- cgit v1.2.3