Changeset 527


Ignore:
Timestamp:
03/23/14 17:26:12 (11 years ago)
Author:
hendrikvanantwerpen
Message:
  • Dropped request module for a simple one we wrote ourselves using the native Node modules. Hopefully this one will not suffer from the problem that sometimes empty bodies are returned even when the statuscode and content-length of the request are ok & present.
  • Handle exceptions better in HTTPResult chain, they were hoisted in unknown responses before. Should we not include them in default processing, because they are special?
Location:
Dev/trunk/src
Files:
1 deleted
12 edited

Legend:

Unmodified
Added
Removed
  • Dev/trunk/src/package.json

    r525 r527  
    88    "passport": "~0.1.18",
    99    "passport-local": "~0.1.6",
    10     "request": "~2.21.0",
    1110    "ya-csv": "~0.9.3",
    1211    "tv4": "~1.0.16"
  • Dev/trunk/src/server/api/categories.js

    r525 r527  
    1515        return couch.get(url,{query:{reduce:true,group:true,group_level:1}})
    1616        .handle({
     17            '-1': _.identity,
    1718            200: function(result) {
    1819                return _.map(result.rows, function(item) {
     
    2930            var sub = 'published' in req.query && 'published';
    3031            getCategories(sub)
     32            .handle({'-1': util.handleException})
    3133            .handle(res.send.bind(res));
    3234        });
     
    3739            var sub = 'published' in req.query && 'published';
    3840            topics.getTopicsWithCategory(category,sub)
     41            .handle({'-1': util.handleException})
    3942            .handle(res.send.bind(res));
    4043        });
  • Dev/trunk/src/server/api/open.js

    r525 r527  
    4444                }
    4545            })
     46            .handle({'-1': util.handleException})
    4647            .handle(res.send.bind(res));
    4748        });
     
    7475                }
    7576            })
     77            .handle({'-1': util.handleException})
    7678            .handle(res.send.bind(res));
    7779        });
     
    112114                }
    113115            })
     116            .handle({'-1': util.handleException})
    114117            .handle(res.send.bind(res));
    115118        });
     
    141144                }
    142145            })
     146            .handle({'-1': util.handleException})
    143147            .handle(res.send.bind(res));
    144148        });
  • Dev/trunk/src/server/api/questions.js

    r525 r527  
    1717        return couch.get(url,{query:query})
    1818        .handle({
     19            '-1': _.identity,
    1920            200: util.handleRowDocs,
    2021            default: util.handleUnknownResponse
     
    2627        return couch.get(url,{query:query})
    2728        .handle({
     29            '-1': _.identity,
    2830            200: util.handleRowDocs,
    2931            default: util.handleUnknownResponse
     
    3537        return couch.get(url,{query:query})
    3638        .handle({
     39            '-1': _.identity,
    3740            200: util.handleRowDictOfValues,
    3841            default: util.handleUnknownResponse
     
    4447        return couch.get(url,{query:query})
    4548        .handle({
     49            '-1': _.identity,
    4650            200: util.handleRowDocs,
    4751            default: util.handleUnknownResponse
     
    5660        return couch.get(url,{query:query})
    5761        .handle({
     62            '-1': _.identity,
    5863            200: util.handleRowDocs,
    5964            default: util.handleUnknownResponse
     
    6772        return couch.get(url)
    6873        .handle({
     74            '-1': _.identity,
    6975            200: util.handleRowDictOfValues,
    7076            default: util.handleUnknownResponse
     
    9399                hr = getQuestions(sub);
    94100            }
    95             hr.handle(res.send.bind(res));
     101            hr.handle({'-1': util.handleException})
     102              .handle(res.send.bind(res));
    96103        });
    97104    app.post('/',
     
    122129                });
    123130            }
    124             hr.handle(res.send.bind(res));
     131            hr.handle({'-1': util.handleException})
     132              .handle(res.send.bind(res));
    125133        });
    126134    app.get('/:id',
     
    160168                });
    161169            }
    162             hr.handle(res.send.bind(res));
     170            hr.handle({'-1': util.handleException})
     171              .handle(res.send.bind(res));
    163172        });
    164173    app.delete('/:id',
  • Dev/trunk/src/server/api/responses.js

    r525 r527  
    1616        return couch.get(url)
    1717        .handle({
     18            '-1': _.identity,
    1819            200: util.handleRowValues,
    1920            default: util.handleUnknownResponse
     
    3031                hr = util.getDocumentsOfType('Response');
    3132            }
    32             hr.handle(res.send.bind(res));
     33            hr.handle({'-1': util.handleException})
     34              .handle(res.send.bind(res));
    3335        });
    3436    app.get('/:id',
     
    4446                return util.postDocument('Response',doc);
    4547            })
     48            .handle({'-1': util.handleException})
    4649            .handle(res.send.bind(res));
    4750        });
     
    6467                hr = util.putDocument(id,rev,'Response',doc);
    6568            }
    66             hr.handle(res.send.bind(res));
     69            hr.handle({'-1': util.handleException})
     70              .handle(res.send.bind(res));
    6771        });
    6872    app.delete('/:id',
  • Dev/trunk/src/server/api/surveyRuns.js

    r525 r527  
    3030                });
    3131            }
    32             hr.handle(res.send.bind(res));
     32            hr.handle({'-1': util.handleException})
     33              .handle(res.send.bind(res));
    3334        });
    3435    app.get('/:id',
     
    5354                hr = util.putDocument(id,rev,'SurveyRun',doc);
    5455            }
    55             hr.handle(res.send.bind(res));
     56            hr.handle({'-1': util.handleException})
     57              .handle(res.send.bind(res));
    5658        });
    5759    app.delete('/:id',
     
    7072                    }
    7173                }
    72             }).handle(res.send.bind(res));
     74            })
     75            .handle({'-1': util.handleException})
     76            .handle(res.send.bind(res));
    7377        });
    7478    app.get('/:id/responses',
     
    7781            var id = req.params.id;
    7882            responses.getResponsesBySurveyRunId(id)
     83            .handle({'-1': util.handleException})
    7984            .handle(res.send.bind(res));
    8085        });
     
    8590            responses.getResponsesBySurveyRunId(id)
    8691            .handle({
     92                '-1': function(result) {
     93                    util.handleException(result)
     94                    .handle(res.send.bind(res));
     95                },
    8796                200: function(responses) {
    8897                    var answers = _.map(responses,function(response){
  • Dev/trunk/src/server/api/surveys.js

    r525 r527  
    2323            couch.get(url)
    2424            .handle({
     25                '-1': _.identity,
    2526                200: function(result) {
    2627                    return _.map(result.rows, function(item) {
     
    3031                default: util.handleUnknownResponse
    3132            })
     33            .handle({'-1': util.handleException})
    3234            .handle(res.send.bind(res));
    3335        });
     
    4547                hr = util.postDocument('Survey',doc);
    4648            }
    47             hr.handle(res.send.bind(res));
     49            hr.handle({'-1': util.handleException})
     50              .handle(res.send.bind(res));
    4851        });
    4952    app.get('/:id',
     
    6669                util.putDocument(id,rev,'Survey',doc)
    6770                .handle({
     71                    '-1': function(result) {
     72                        util.handleException(result)
     73                        .handle(res.send.bind(res));
     74                    },
    6875                    201: function(doc) {
    6976                        res.set({
  • Dev/trunk/src/server/api/topics.js

    r525 r527  
    1414        return couch.get(url,{query:{reduce:true,group:true}})
    1515        .handle({
     16            '-1': _.identity,
    1617            200: function(result) {
    1718                return _.map(result.rows, function(item) { return {name:item.key, count:item.value}; });
     
    2425        return couch.get(url,{query:{reduce:true,group:true,group_level:2,startkey:[category],endkey:[category,{}]}})
    2526        .handle({
     27            '-1': _.identity,
    2628            200: function(result) {
    2729                return _.map(result.rows, function(item) { return { name:item.key[1], count:item.value }; });
     
    4143                hr = getTopics(sub);
    4244            }
    43             hr.handle(res.send.bind(res));
     45            hr.handle({'-1': util.handleException})
     46              .handle(res.send.bind(res));
    4447        });
    4548
  • Dev/trunk/src/server/api/util.js

    r525 r527  
    3434    };
    3535
     36    var handleException = exports.handleException = function(error) {
     37        return new HTTPResult(500,{
     38            reason: error.message,
     39            filename: error.filename,
     40            lineNumber: error.lineNumber
     41        });
     42    };
    3643    var handleUnknownResponse = exports.handleUnknownResponse = function(status,error) {
    3744        return new HTTPResult(500,{error: error.reason});
     
    7784        return couch.get(url)
    7885        .handle({
     86            '-1': _.identity,
    7987            200: handleRowValues,
    8088            404: function() { return {error: "Cannot find collection of type "+type}; },
     
    8997        return couch.get(id,opts)
    9098        .handle({
     99            '-1': _.identity,
    91100            200: function(doc){
    92101                if ( doc.type !== type ) {
     
    114123            return couch.put(id,doc,opts)
    115124            .handle({
     125                '-1': _.identity,
    116126                201: function(res){
    117127                    doc._id = res.id;
     
    133143            return couch.delete(id,opts)
    134144            .handle({
     145                '-1': _.identity,
    135146                200: identity,
    136147                409: function(error) {
     
    149160            return couch.post(doc)
    150161            .handle({
     162                '-1': _.identity,
    151163                201: function(response) {
    152164                    doc._id = response.id;
     
    164176        return function(req,res) {
    165177            getDocumentsOfType(type)
     178            .handle({'-1': handleException})
    166179            .handle(res.send.bind(res));
    167180        };
     
    173186            getDocument(id,rev,type)
    174187            .handle({
     188                '-1': function(result) {
     189                    handleException(result)
     190                    .handle(res.send.bind(res));
     191                },
    175192                200: function(doc){
    176193                    res.set({
     
    189206            putDocument(id,rev,type,doc)
    190207            .handle({
     208                '-1': function(result) {
     209                    handleException(result)
     210                    .handle(res.send.bind(res));
     211                },
    191212                201: function(doc) {
    192213                    res.set({
     
    204225            var rev = etags.parse(req.header('If-Match'))[0] || (doc && doc._rev);
    205226            deleteDocument(id,rev)
     227            .handle({'-1': handleException})
    206228            .handle(res.send.bind(res));
    207229        };
     
    212234            postDocument(type,doc)
    213235            .handle({
     236                '-1': function(result) {
     237                    handleException(result)
     238                    .handle(res.send.bind(res));
     239                },
    214240                201: function(doc) {
    215241                    res.set({
  • Dev/trunk/src/server/util/couch.js

    r525 r527  
    11var request = require('./request')
    22  , _ = require('underscore')
     3  , HTTPResult = require('./http-result')
    34  ;
    45
     
    4243}
    4344
    44 function couchRequest (method, url, body, opts) {
     45function couchRequest (method, url, body, opts, retries) {
     46    body = body || {};
     47    retries = _.isNumber(retries) ? retries : 3;
    4548    var options = {
    4649        method: method,
    47         headers: {
    48             'Content-Type': 'application/json; charset=utf-8',
    49             'Accept': 'application/json'
    50         },
    51         body: JSON.stringify(stringifyFunctions(body || {}))
     50        headers: {},
     51        body: stringifyFunctions(body),
     52        json: true,
     53        timeout: 5000
    5254    };
    5355    if (opts) {
     
    5658    }
    5759    return request(url, options)
    58         .handle({
    59             '-1': _.identity,
    60             default: function(status,result) { return JSON.parse(result); }
    61         });
     60    .handle(function(status,body){
     61        if ( _.isUndefined(body) ) {
     62            if ( retries > 0) {
     63                return couchRequest(method,url,body,opts,retries-1);
     64            } else {
     65                return new HTTPResult(-1,new Error("No response after several retries."));
     66            }
     67        } else {
     68            return body;
     69        }
     70    });
    6271}
    6372
  • Dev/trunk/src/server/util/http-result.js

    r525 r527  
    3939// _fire :: Handler -> ()
    4040HTTPResult.prototype._fire = function(handler) {
    41     var ret = handler.f(this.status,this.result);
    42     if ( ret instanceof HTTPResult ) {
    43         ret.handle(handler.next.set.bind(handler.next));
    44     } else {
    45         handler.next.set(this.status,ret);
     41    try {
     42        var ret = handler.f(this.status,this.result);
     43        if ( ret instanceof HTTPResult ) {
     44            ret.handle(handler.next.set.bind(handler.next));
     45        } else {
     46            handler.next.set(this.status,ret);
     47        }
     48    } catch(ex) {
     49        console.log('HTTPResult','exception',ex.toString(),ex.stack);
     50        handler.next.set(-1,ex);
    4651    }
    4752};
  • Dev/trunk/src/server/util/request.js

    r525 r527  
    11var HTTPResult = require('./http-result')
    2   , request = require('request')
    3   , url = require('url')
     2  , HTTP = require('http')
     3  , HTTPS = require('https')
     4  , URL = require('url')
    45  , _ = require('underscore')
    56  ;
     
    910    var result = new HTTPResult();
    1011   
    11     options = options ? _.clone(options) : {};
    12     options.uri = url || options.uri;
    13    
    14     request(options,function(err,res,body){
    15         if ( err ) {
    16             result.set(-1,err);
    17         } else {
    18             result.set(res.statusCode,body);
    19         }
     12    var body = options.body;
     13    delete options.body;
     14
     15    var json = options.json;
     16    delete options.json;
     17
     18    var reqOpts = _.isString(url) ? URL.parse(url) : url;
     19    _.extend(reqOpts,options);
     20    if ( json === true ) {
     21        options.headers = options.headers || {};
     22        _.extend(options.headers,{
     23            'Content-Type': 'application/json',
     24            'Accepts': 'application/json'
     25        });
     26    }
     27
     28    var protocol = reqOpts.protocol;
     29    var httpModule;
     30    if ( protocol === 'http:' ) {
     31        httpModule = HTTP;
     32    } else if ( protocol === 'https:' ) {
     33        httpModule = HTTPS;
     34    } else {
     35        throw new Error("Unknown protocol "+protocol);
     36    }
     37
     38    var req = httpModule.request(reqOpts);
     39    req.on('response', function(response) {
     40        var data = "";
     41        response.setEncoding('utf8');
     42        response.on('data', function(chunk) {
     43            data += chunk;
     44        });
     45        response.on('end', function() {
     46            var body;
     47            try {
     48                if ( json ) {
     49                    if ( data ) { body = JSON.parse(data); }
     50                } else {
     51                    body = data;
     52                }
     53                result.set(response.statusCode,body);
     54            } catch (ex) {
     55                console.log('error',data);
     56                result.set(-1,ex);
     57            }
     58        });
     59        response.on('error', function(error) {
     60            result.set(-1,error);
     61        });
    2062    });
     63    req.on('error', function(error){
     64        result.set(-1,error);
     65    });
     66
     67    if ( body ) {
     68        req.write(json ? JSON.stringify(body) : body);
     69    }
     70    req.end();
    2171
    2272    return result;
Note: See TracChangeset for help on using the changeset viewer.