diff options
author | Trygve Laugstøl <trygvis@inamo.no> | 2012-07-04 13:34:51 +0200 |
---|---|---|
committer | Trygve Laugstøl <trygvis@inamo.no> | 2012-07-04 13:34:51 +0200 |
commit | b710f1c071d75d4b028c61bb6f3927c2cd4aadf5 (patch) | |
tree | 556a7f700a0a56b1f3ebfd7dd31f9093b96a01af /routes | |
parent | 9b1195430a2dc636e8325ecbb7445855d9d98259 (diff) | |
download | collection-json-explorer-b710f1c071d75d4b028c61bb6f3927c2cd4aadf5.tar.gz collection-json-explorer-b710f1c071d75d4b028c61bb6f3927c2cd4aadf5.tar.bz2 collection-json-explorer-b710f1c071d75d4b028c61bb6f3927c2cd4aadf5.tar.xz collection-json-explorer-b710f1c071d75d4b028c61bb6f3927c2cd4aadf5.zip |
o Always showing the http response, even when the body couldn't be decoded.
Diffstat (limited to 'routes')
-rw-r--r-- | routes/index.js | 68 |
1 files changed, 37 insertions, 31 deletions
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); |