Skip to content

Commit 7c4ced7

Browse files
committed
#127 Improve Error Handling for Failed DefectDojo API Requests
Two Problems: 1. We do not pass consequently the causing exception to our own custom exception. 2. We do not catch all possible REST client runtime exceptions. Fixed by consequent logging and passing ofthe origin exception as cause of our own excpetion. Also catching a more generic exception type totach all possible errors. Signed-off-by: Sven Strittmatter <sven.strittmatter@iteratec.com>
1 parent 8d888bd commit 7c4ced7

File tree

3 files changed

+20
-13
lines changed

3 files changed

+20
-13
lines changed

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
3232
import org.springframework.util.LinkedMultiValueMap;
3333
import org.springframework.util.MultiValueMap;
34-
import org.springframework.web.client.HttpClientErrorException;
34+
import org.springframework.web.client.RestClientException;
3535
import org.springframework.web.client.RestTemplate;
3636

3737
import java.nio.charset.StandardCharsets;
@@ -124,7 +124,7 @@ public String getFilename() {
124124

125125
final var payload = new HttpEntity<MultiValueMap<String, Object>>(body, headers);
126126
return exchangeRequest(endpoint, payload);
127-
} catch (HttpClientErrorException e) {
127+
} catch (RestClientException e) {
128128
log.error("Exception while attaching findings to engagement: {}", e.getMessage());
129129
throw new PersistenceException("Failed to attach findings to engagement.", e);
130130
}

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

+15-8
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import com.fasterxml.jackson.databind.cfg.CoercionInputShape;
1313
import io.securecodebox.persistence.defectdojo.config.Config;
1414
import io.securecodebox.persistence.defectdojo.exception.LoopException;
15+
import io.securecodebox.persistence.defectdojo.exception.PersistenceException;
1516
import io.securecodebox.persistence.defectdojo.http.Foo;
1617
import io.securecodebox.persistence.defectdojo.http.ProxyConfigFactory;
1718
import io.securecodebox.persistence.defectdojo.model.Engagement;
@@ -29,6 +30,7 @@
2930
import org.springframework.http.converter.StringHttpMessageConverter;
3031
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
3132
import org.springframework.util.LinkedMultiValueMap;
33+
import org.springframework.web.client.RestClientException;
3234
import org.springframework.web.client.RestTemplate;
3335
import org.springframework.web.util.UriComponentsBuilder;
3436

@@ -73,14 +75,19 @@ public final T get(long id) {
7375

7476
final var url = this.config.getUrl() + API_PREFIX + this.getUrlPath() + "/" + id;
7577
log.debug("Requesting URL: {}", url);
76-
ResponseEntity<T> response = restTemplate.exchange(
77-
url,
78-
HttpMethod.GET,
79-
payload,
80-
getModelClass()
81-
);
82-
83-
return response.getBody();
78+
try {
79+
ResponseEntity<T> response = restTemplate.exchange(
80+
url,
81+
HttpMethod.GET,
82+
payload,
83+
getModelClass()
84+
);
85+
86+
return response.getBody();
87+
} catch (RestClientException e) {
88+
log.error("Exception while getting data: {}", e.getMessage());
89+
throw new PersistenceException("Failed to get data.", e);
90+
}
8491
}
8592

8693
@Override

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
2323
import org.springframework.util.LinkedMultiValueMap;
2424
import org.springframework.util.MultiValueMap;
25-
import org.springframework.web.client.HttpClientErrorException;
25+
import org.springframework.web.client.RestClientException;
2626
import org.springframework.web.client.RestTemplate;
2727

2828
import java.nio.charset.StandardCharsets;
@@ -94,8 +94,8 @@ public String getFilename() {
9494
var payload = new HttpEntity<>(mvn, headers);
9595

9696
return restTemplate.exchange(config.getUrl() + "/api/v2/" + endpoint + "/", HttpMethod.POST, payload, ImportScanResponse.class).getBody();
97-
} catch (HttpClientErrorException e) {
98-
throw new PersistenceException("Failed to attach findings to engagement.");
97+
} catch (RestClientException e) {
98+
throw new PersistenceException("Failed to attach findings to engagement.", e);
9999
}
100100
}
101101

0 commit comments

Comments
 (0)