Skip to content

Commit

Permalink
Handle all REST exception in communication between services
Browse files Browse the repository at this point in the history
  • Loading branch information
pwargulak committed Sep 3, 2024
1 parent 4abeafe commit 46e7f2b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.HttpStatusCodeException;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestOperations;
import org.springframework.web.client.RestTemplate;

Expand Down Expand Up @@ -140,6 +141,8 @@ public T findOne(String resourceUrl, RequestParameters parameters, Class<T> type
} else {
throw buildDataRetrievalException(ex);
}
} catch (RestClientException ex) {
throw buildDataRetrievalException(ex);
}
}

Expand All @@ -160,7 +163,7 @@ protected Collection<T> findAll(String resourceUrl, Map<String, Object> paramete
() -> doListRequest(url, params, HttpMethod.GET, getArrayResultClass())
);
return new ArrayList<>(Arrays.asList(responseEntity.getBody()));
} catch (HttpStatusCodeException ex) {
} catch (RestClientException ex) {
throw buildDataRetrievalException(ex);
}
}
Expand All @@ -183,7 +186,7 @@ protected <P> ServiceResponse<List<P>> tryFindAll(String resourceUrl, Class<P[]>
List<P> list = Stream.of(response.getBody()).collect(Collectors.toList());
return new ServiceResponse<>(list, response.getHeaders(), true);
}
} catch (HttpStatusCodeException ex) {
} catch (RestClientException ex) {
throw buildDataRetrievalException(ex);
}
}
Expand Down Expand Up @@ -247,7 +250,7 @@ protected <P> Page<P> getPage(String resourceUrl, RequestParameters parameters,
);
return response.getBody();

} catch (HttpStatusCodeException ex) {
} catch (RestClientException ex) {
throw buildDataRetrievalException(ex);
}
}
Expand Down Expand Up @@ -326,7 +329,7 @@ private <E> ResponseEntity<PageDto<E>> doPageRequest(String url,
return new ResponseEntity<>(body, HttpStatus.OK);
}

private DataRetrievalException buildDataRetrievalException(HttpStatusCodeException ex) {
private DataRetrievalException buildDataRetrievalException(RestClientException ex) {
return new DataRetrievalException(getResultClass().getSimpleName(), ex);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@

import lombok.Getter;
import org.springframework.http.HttpStatus;
import org.springframework.web.client.HttpStatusCodeException;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestClientResponseException;

/**
* Signals we were unable to retrieve reference data
Expand All @@ -38,10 +39,15 @@ public class DataRetrievalException extends RuntimeException {
* Constructs the exception.
*
* @param resource the resource that we were trying to retrieve
* @param ex exception with status code and response body from server
* @param ex rest client exception, may contain response from server with code and body
*/
public DataRetrievalException(String resource, HttpStatusCodeException ex) {
this(resource, ex.getStatusCode(), ex.getResponseBodyAsString());
public DataRetrievalException(String resource, RestClientException ex) {
this(resource, ex instanceof RestClientResponseException
? HttpStatus.resolve(((RestClientResponseException) ex).getRawStatusCode())
: null,
ex instanceof RestClientResponseException
? ((RestClientResponseException) ex).getResponseBodyAsString()
: ex.getLocalizedMessage());
}

/**
Expand All @@ -53,7 +59,7 @@ public DataRetrievalException(String resource, HttpStatusCodeException ex) {
*/
public DataRetrievalException(String resource, HttpStatus status, String response) {
super(String.format("Unable to retrieve %s. Error code: %d, response message: %s",
resource, status.value(), response));
resource, status != null ? status.value() : 0, response));
this.resource = resource;
this.status = status;
this.response = response;
Expand Down

0 comments on commit 46e7f2b

Please sign in to comment.