Created
September 13, 2011 17:10
-
-
Save Raynos/1214356 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var request = require("request-middleware"); | |
var errorFunction = function(err, res, body, next) { | |
if (err.type === "timeout") { | |
this.repeat(); | |
} else { | |
next(err); | |
} | |
}; | |
var errorStack = middleware(errorFunction); | |
// either use middleware on the request | |
request("http://failing-server", errorStack, function(res, body) { | |
// do things | |
}); | |
// or use it globally | |
request.use(errorFunction); | |
// or generate a middleware stack with request | |
var makeRequest = function(options, next) { | |
var store = this; | |
request(options, function(err, res, body) { | |
if (err) return next(err); | |
store.set("res", res); | |
store.set("body", body); | |
next(); | |
}); | |
}; | |
var repeatOnTimeout = function(stack) { | |
return function(err, options, next) { | |
if (err === "timeout") { | |
return stack.handle(options); | |
} | |
next(err); | |
}; | |
} | |
var requestStack = middleware(function() { | |
makeRequest, | |
repeatOnTimeout(requestStack), | |
function (err, options, next) { | |
// handle errors | |
}, | |
function (options) { | |
var res = this.get("res"), body = this.get("body"); | |
// handle res,body | |
} | |
}); | |
requestStack.handle("http://failing-server"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment