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 ++++++++++++++++++++++++++++++-------------------------- views/data.jade | 45 +++++++++++++++++-------------------- views/index.jade | 5 +++-- 3 files changed, 60 insertions(+), 58 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); diff --git a/views/data.jade b/views/data.jade index 9f64ddf..edaa4d6 100644 --- a/views/data.jade +++ b/views/data.jade @@ -128,13 +128,16 @@ block queries div(class='controls') input(type='submit') Execute -block headers - pre - table - each value, key in headers - tr - td #{key}: - td #{value} +block http_response + div(class='row-fluid') + pre + table + each value, key in headers + tr + td #{key}: + td #{value} + | + | #{rawBody} block navbar div(class='navbar navbar-fixed-top') @@ -164,7 +167,6 @@ block sidebar ul(class='nav nav-list') if typeof err != 'undefined' li(class='nav-header'): a(href='#error') Error - li(class='nav-header'): a(href='#raw-body') Raw Body else li(class='nav-header'): a(href='#meta') Meta each link, i in collection.links @@ -177,22 +179,19 @@ block sidebar each query, i in collection.queries li: a(href='#query-' + (i + 1)) mixin get_name(query, 'Query', i) - li(class='nav-header'): a(href='#headers') Headers li(class='nav-header'): a(href='#formatted-body') Formatted Body - li(class='nav-header'): a(href='#raw-body') Raw Body + li(class='nav-header'): a(href='#http-response') HTTP Response block inner_content if typeof err != 'undefined' section(id='error') - div(class='page-error') + div(class='page-header') h1 Error - p Error rendering #{url} + div(class='row-fluid') + p Error rendering: #{url} + a(class='btn btn-primary btn-mini', href=urlgenerator.render(url)) Retry p= err - section(id='raw-body') - div(class='page-header') - h1 Raw Body - pre= rawBody else section(id='meta') div(class='page-header') @@ -208,23 +207,19 @@ block inner_content div(class='page-header') h1 Queries block queries - section(id='headers') - div(class='page-header') - h1 Headers - block headers section(id='formatted-body') div(class='page-header') h1 Formatted Body pre= formattedBody - section(id='raw-body') - div(class='page-header') - h1 Raw Body - pre= rawBody + + section(id='http-response') + div(class='page-header') + h1 HTTP Response + block http_response block content div(class='row-fluid') div(class='span3') block sidebar div(class='span9') - - console.log('fuck') block inner_content diff --git a/views/index.jade b/views/index.jade index 913b131..f9ef5d2 100644 --- a/views/index.jade +++ b/views/index.jade @@ -64,7 +64,7 @@ block inner_content h3 The Source Code p The source code is available - a(href='trygvis.dyndns.org/~trygvis/git/2012/06/collection+json-explorer.git') here + a(href='http://trygvis.dyndns.org/~trygvis/git/2012/06/collection+json-explorer.git') here | . div(class='span4') @@ -77,7 +77,8 @@ block inner_content |explorer: a(href='http://' + host + '/render?url=http%3A%2F%2Femployee.herokuapp.com', class='label label-info') explore now! - p The specification contains a few example collections too which you can explore: + p The specification contains a few example collections too which + | you can explore: - var examples = [ "minimal", "collection", "item", "queries", "template", "error" ] each example, i in examples - var u='http://' + host + '/render?url=' + encodeURIComponent('http://' + host + '/examples/from-spec/' + example + '.collection+json') -- cgit v1.2.3