Question

i'm trying to make an async call to a domain. The following code works well if i specify a valid address with json response, but when the address is not valid, i want to be able to catch any possible exceptions.

How can i catch the returned exception?

Here an extract from stacktrace:

Message: Invalid JSON String

...

http.AsyncHTTPBuilder - Exception thrown from response delegate: groovyx.net.http.HTTPBuilder$RequestConfigDelegate@420db81e

Here the code:

def http = new AsyncHTTPBuilder( poolSize : 1,
                                 contentType : ContentType.JSON )

def futureResult

futureResult = http.request( "http://www.notexistingdomainxyzwq.com/",
                             Method.GET,
                             ContentType.JSON ) {
    response.success = { resp, json ->
        log.info("SUCCESS")             
    }

    response.failure = { resp, json ->
        log.info("ERROR")
    }

}

log.info("Call started");

try {
    while (!futureResult.done) {
        log.info('waiting...')
        log.info("DONE: ${futureResult.done}")
        Thread.sleep(1000)
    }
} catch(ex) {
    log.error("EXCE ${ex}")
}

log.info("Call completed")
Was it helpful?

Solution

If you call futureResult.get() to block and wait for the result, this will throw the exception which you can catch:

try {
    def result = futureResult.get()
    log.info( "Done: $result" )
} catch(ex) {
    log.error("EXCE ${ex}")
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top