Skip to content
This repository has been archived by the owner on May 11, 2021. It is now read-only.

Unwrapping ExecutionException when executed synchronously. Fixes #139 #142

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import org.springframework.http.HttpMethod
import org.springframework.http.ResponseEntity
import org.springframework.web.client.RestOperations

import java.util.concurrent.ExecutionException

import static com.ofg.infrastructure.web.resttemplate.fluent.common.response.executor.UrlParsingUtils.appendPathToHost

/**
Expand All @@ -28,7 +30,11 @@ final class RestExecutor<T> {
}

ResponseEntity<T> exchange(HttpMethod httpMethod, Map params, Class<T> responseType) {
return exchangeInternal(params, httpMethod, responseType).get()
try {
return exchangeInternal(params, httpMethod, responseType).get()
} catch (ExecutionException e) {
throw e.cause
}
}

ListenableFuture<ResponseEntity<T>> exchangeAsync(HttpMethod httpMethod, Map params, Class<T> responseType) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.ofg.infrastructure.web.resttemplate.fluent.common.response.executor
import com.nurkiewicz.asyncretry.SyncRetryExecutor
import com.ofg.infrastructure.web.resttemplate.custom.RestTemplate
import org.springframework.http.HttpMethod
import org.springframework.web.client.ResourceAccessException
import spock.lang.Specification

class RestExecutorTest extends Specification {
Expand All @@ -19,4 +20,16 @@ class RestExecutorTest extends Specification {
e.message.contains("retryUsing")
}

def 'should propagate original RestTemplate exception'() {
given:
RestExecutor executor = new RestExecutor(
new RestTemplate(), SyncRetryExecutor.INSTANCE)
when:
executor.exchange(HttpMethod.GET, [host: 'http://localhost:7777', url: '/api'.toURI()], Object)

then:
ResourceAccessException e = thrown(ResourceAccessException)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is one of the examples when IMHO def is readable enough and much more consist. Wouldn't you agree @marcingrzejszczak?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

def in the method name of test? Of course it does - I also use def in that case

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, the line I commented :).

ResourceAccessException e = thrown(ResourceAccessException)
e.message?.contains('localhost:7777')

vs.

def e = thrown(ResourceAccessException)
e.message?.contains('localhost:7777')

especially that it is used in the next line.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:D I'm not an evangelist of anti-def. Everything is ok if you use common sense. If you have such a case then it does make sense to use def :)

e.message.contains('localhost:7777')
}

}