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?
File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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
Note: See TracChangeset for help on using the changeset viewer.