Skip to content

Commit

Permalink
#127 Also Handle HTTP Client Exception for All Other HTTP Methods
Browse files Browse the repository at this point in the history
In previous commits only GET requests were covered with proper
exception handling.

This adds rethrow of our own custom exception with the originating
error as cause, like already implemented for GET requests.

Signed-off-by: Sven Strittmatter <[email protected]>
  • Loading branch information
Weltraumschaf committed Jul 27, 2024
1 parent 3856a3f commit c2daf60
Showing 1 changed file with 47 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ public final T get(long id) {

final var url = this.config.getUrl() + API_PREFIX + this.getUrlPath() + "/" + id;
log.debug("Requesting URL: {}", url);

try {
ResponseEntity<T> response = restTemplate.exchange(
url,
Expand All @@ -85,8 +86,8 @@ public final T get(long id) {

return response.getBody();
} catch (RestClientException e) {
log.error("Exception while getting data: {}", e.getMessage());
throw new PersistenceException("Failed to get data.", e);
log.error("Exception while doing a GET request to DefectDojo API: {}", e.getMessage());
throw new PersistenceException("Failed to do a GET to DefectDojo API!", e);
}
}

Expand Down Expand Up @@ -140,27 +141,53 @@ public final T create(@NonNull T object) {
var restTemplate = this.getRestTemplate();
HttpEntity<T> payload = new HttpEntity<>(object, getDefectDojoAuthorizationHeaders());

ResponseEntity<T> response = restTemplate.exchange(this.config.getUrl() + API_PREFIX + getUrlPath() + "/", HttpMethod.POST, payload, getModelClass());
return response.getBody();
try {
ResponseEntity<T> response = restTemplate.exchange(
this.config.getUrl() + API_PREFIX + getUrlPath() + "/",
HttpMethod.POST,
payload,
getModelClass());
return response.getBody();
} catch (RestClientException e) {
log.error("Exception while doing a POST request to DefectDojo API: {}", e.getMessage());
throw new PersistenceException("Failed to do a POST to DefectDojo API!", e);
}
}

@Override
public final void delete(long id) {
var restTemplate = this.getRestTemplate();
HttpEntity<String> payload = new HttpEntity<>(getDefectDojoAuthorizationHeaders());

restTemplate.exchange(this.config.getUrl() + API_PREFIX + getUrlPath() + "/" + id + "/", HttpMethod.DELETE, payload, String.class);
try {
restTemplate.exchange(
this.config.getUrl() + API_PREFIX + getUrlPath() + "/" + id + "/",
HttpMethod.DELETE,
payload,
String.class);
} catch (RestClientException e) {
log.error("Exception while doing a DELETE request to DefectDojo API: {}", e.getMessage());
throw new PersistenceException("Failed to do a DELETE to DefectDojo API!", e);
}
}

@Override
public final T update(@NonNull T object, long id) {
var restTemplate = this.getRestTemplate();
HttpEntity<T> payload = new HttpEntity<>(object, getDefectDojoAuthorizationHeaders());

ResponseEntity<T> response = restTemplate.exchange(this.config.getUrl() + API_PREFIX + getUrlPath() + "/" + id + "/", HttpMethod.PUT, payload, getModelClass());
return response.getBody();
try {
ResponseEntity<T> response = restTemplate.exchange(this.config.getUrl() + API_PREFIX + getUrlPath() + "/" + id + "/",
HttpMethod.PUT,
payload,
getModelClass());
return response.getBody();
} catch (RestClientException e) {
log.error("Exception while doing a PUT request to DefectDojo API: {}", e.getMessage());
throw new PersistenceException("Failed to do a PUT to DefectDojo API!", e);
}
}

/**
* Get the URL path for the REST endpoint relative to {@link #API_PREFIX}
*
Expand Down Expand Up @@ -222,13 +249,18 @@ protected PaginatedResult<T> internalSearch(Map<String, Object> queryParams, lon
log.debug("Requesting URL: {}", url);
var uriBuilder = UriComponentsBuilder.fromUri(url).queryParams(multiValueMap);

ResponseEntity<String> responseString = restTemplate.exchange(
uriBuilder.build(mutableQueryParams),
HttpMethod.GET,
payload,
String.class
);
try {
ResponseEntity<String> responseString = restTemplate.exchange(
uriBuilder.build(mutableQueryParams),
HttpMethod.GET,
payload,
String.class
);

return deserializeList(responseString.getBody());
return deserializeList(responseString.getBody());
} catch (RestClientException e) {
log.error("Exception while doing a GET request to DefectDojo API: {}", e.getMessage());
throw new PersistenceException("Failed to do a GET to DefectDojo API!", e);
}
}
}

0 comments on commit c2daf60

Please sign in to comment.