Skip to content

Commit c2daf60

Browse files
committed
#127 Also Handle HTTP Client Exception for All Other HTTP Methods
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 <sven.strittmatter@iteratec.com>
1 parent 3856a3f commit c2daf60

File tree

1 file changed

+47
-15
lines changed

1 file changed

+47
-15
lines changed

src/main/java/io/securecodebox/persistence/defectdojo/service/GenericDefectDojoService.java

+47-15
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ public final T get(long id) {
7575

7676
final var url = this.config.getUrl() + API_PREFIX + this.getUrlPath() + "/" + id;
7777
log.debug("Requesting URL: {}", url);
78+
7879
try {
7980
ResponseEntity<T> response = restTemplate.exchange(
8081
url,
@@ -85,8 +86,8 @@ public final T get(long id) {
8586

8687
return response.getBody();
8788
} catch (RestClientException e) {
88-
log.error("Exception while getting data: {}", e.getMessage());
89-
throw new PersistenceException("Failed to get data.", e);
89+
log.error("Exception while doing a GET request to DefectDojo API: {}", e.getMessage());
90+
throw new PersistenceException("Failed to do a GET to DefectDojo API!", e);
9091
}
9192
}
9293

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

143-
ResponseEntity<T> response = restTemplate.exchange(this.config.getUrl() + API_PREFIX + getUrlPath() + "/", HttpMethod.POST, payload, getModelClass());
144-
return response.getBody();
144+
try {
145+
ResponseEntity<T> response = restTemplate.exchange(
146+
this.config.getUrl() + API_PREFIX + getUrlPath() + "/",
147+
HttpMethod.POST,
148+
payload,
149+
getModelClass());
150+
return response.getBody();
151+
} catch (RestClientException e) {
152+
log.error("Exception while doing a POST request to DefectDojo API: {}", e.getMessage());
153+
throw new PersistenceException("Failed to do a POST to DefectDojo API!", e);
154+
}
145155
}
146156

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

152-
restTemplate.exchange(this.config.getUrl() + API_PREFIX + getUrlPath() + "/" + id + "/", HttpMethod.DELETE, payload, String.class);
162+
try {
163+
restTemplate.exchange(
164+
this.config.getUrl() + API_PREFIX + getUrlPath() + "/" + id + "/",
165+
HttpMethod.DELETE,
166+
payload,
167+
String.class);
168+
} catch (RestClientException e) {
169+
log.error("Exception while doing a DELETE request to DefectDojo API: {}", e.getMessage());
170+
throw new PersistenceException("Failed to do a DELETE to DefectDojo API!", e);
171+
}
153172
}
154173

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

160-
ResponseEntity<T> response = restTemplate.exchange(this.config.getUrl() + API_PREFIX + getUrlPath() + "/" + id + "/", HttpMethod.PUT, payload, getModelClass());
161-
return response.getBody();
179+
try {
180+
ResponseEntity<T> response = restTemplate.exchange(this.config.getUrl() + API_PREFIX + getUrlPath() + "/" + id + "/",
181+
HttpMethod.PUT,
182+
payload,
183+
getModelClass());
184+
return response.getBody();
185+
} catch (RestClientException e) {
186+
log.error("Exception while doing a PUT request to DefectDojo API: {}", e.getMessage());
187+
throw new PersistenceException("Failed to do a PUT to DefectDojo API!", e);
188+
}
162189
}
163-
190+
164191
/**
165192
* Get the URL path for the REST endpoint relative to {@link #API_PREFIX}
166193
*
@@ -222,13 +249,18 @@ protected PaginatedResult<T> internalSearch(Map<String, Object> queryParams, lon
222249
log.debug("Requesting URL: {}", url);
223250
var uriBuilder = UriComponentsBuilder.fromUri(url).queryParams(multiValueMap);
224251

225-
ResponseEntity<String> responseString = restTemplate.exchange(
226-
uriBuilder.build(mutableQueryParams),
227-
HttpMethod.GET,
228-
payload,
229-
String.class
230-
);
252+
try {
253+
ResponseEntity<String> responseString = restTemplate.exchange(
254+
uriBuilder.build(mutableQueryParams),
255+
HttpMethod.GET,
256+
payload,
257+
String.class
258+
);
231259

232-
return deserializeList(responseString.getBody());
260+
return deserializeList(responseString.getBody());
261+
} catch (RestClientException e) {
262+
log.error("Exception while doing a GET request to DefectDojo API: {}", e.getMessage());
263+
throw new PersistenceException("Failed to do a GET to DefectDojo API!", e);
264+
}
233265
}
234266
}

0 commit comments

Comments
 (0)