Details
-
Type:
New Feature
-
Status:
In Progress
-
Priority:
Minor
-
Resolution: Unresolved
-
Affects Version/s: HTTPBuilder-0.5.1
-
Fix Version/s: HTTPBuilder-0.5.3
-
Component/s: HttpBuilder
-
Labels:None
-
Number of attachments :
Description
Quote email from Rafal...
Why is there no request.exception = {}
closure available in HTTPBuilder? The failure handler doesn't allow you
to trap runtime exceptions that may occur in something like parsing JSON
or XML. I for one wanted a way to log the bogus response.text so to make
it easier to troubleshoot issues.
I ended up extending HTTPBuilder and overriding doRequest() to provide
my own request.exception handler. I'd love to see some sort of exception
handling available in the library by default.
Here is what I ended up doing, its a bit hackish but I couldn't see a
more elegant way to do what I wanted.
@Override protected Object doRequest(URI uri, Method method, Object
contentType, Closure configClosure) {
def delegateRequest
def delegateResponse
// hackish, but only way to gain access to the configClosure
request& response objects
def newClosure = {
delegateRequest = request
delegateResponse = response
configClosure.delegate = delegate
configClosure()
}
try {
return super.doRequest(uri, method, contentType, newClosure)
} catch (Exception e) {
if ( delegateResponse.exception != null ) {
delegateResponse.exception(e)
} else {
log.error("Exception occured in request to ${uri}")
log.error("Request returned the following response text")
log.error(delegateResponse.text() )
throw e
}
}
}
Actually that method above probably wont work, and needs to be more like this, tho I doubt it really matters for the purpose of this enhancement. I just wanted to correct it, as it was wrong
@Override protected Object doRequest(URI uri, Method method, Object contentType, Closure configClosure) { def delegateRequest def delegateResponse // hackish, but only way to gain access to the configClosure request & response objects def newClosure = { delegateRequest = request delegateResponse = response configClosure.delegate = delegate configClosure() } try { return super.doRequest(uri, method, contentType, newClosure) } catch (Exception e) { if ( delegateResponse.exception != null ) { delegateResponse.exception(e) } else { // we're expecting a response object in the exception HttpResponseDecorator httpResponse = e.response log.error("Exception occured in request to ${uri.path}/${method.toString()}") throw e } } }