summaryrefslogtreecommitdiffstats
path: root/common/src
diff options
context:
space:
mode:
authorMichaelMorris <michael.morris@est.tech>2019-10-03 17:45:05 +0100
committerMichaelMorris <michael.morris@est.tech>2019-10-09 09:20:49 +0100
commitfbf6300274558ffe323bf66bb5b23c72c350d3ee (patch)
tree052c66dd84c7f12580fc176535f00cd2814ece15 /common/src
parent6392515d91c00df8cb925ec9e99273a2f9e4744f (diff)
Improved error handling
Change-Id: Iac436f6a950bf61ac6321ef1d427a7bb14774e30 Issue-ID: SO-2395 Signed-off-by: MichaelMorris <michael.morris@est.tech>
Diffstat (limited to 'common/src')
-rw-r--r--common/src/main/java/org/onap/so/rest/exceptions/HttpResouceNotFoundException.java31
-rw-r--r--common/src/main/java/org/onap/so/rest/exceptions/RestProcessingException.java16
-rw-r--r--common/src/main/java/org/onap/so/rest/service/HttpRestServiceProviderImpl.java18
-rw-r--r--common/src/test/java/org/onap/so/rest/service/HttpRestServiceProviderImplTest.java5
4 files changed, 61 insertions, 9 deletions
diff --git a/common/src/main/java/org/onap/so/rest/exceptions/HttpResouceNotFoundException.java b/common/src/main/java/org/onap/so/rest/exceptions/HttpResouceNotFoundException.java
new file mode 100644
index 0000000000..e7b7b72957
--- /dev/null
+++ b/common/src/main/java/org/onap/so/rest/exceptions/HttpResouceNotFoundException.java
@@ -0,0 +1,31 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * Copyright (C) 2019 Nordix Foundation.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.so.rest.exceptions;
+
+
+public class HttpResouceNotFoundException extends RuntimeException {
+
+ private static final long serialVersionUID = 9007892558312387355L;
+
+ public HttpResouceNotFoundException(final String message) {
+ super(message);
+ }
+}
diff --git a/common/src/main/java/org/onap/so/rest/exceptions/RestProcessingException.java b/common/src/main/java/org/onap/so/rest/exceptions/RestProcessingException.java
index e8ce00c7e5..5d62d8488a 100644
--- a/common/src/main/java/org/onap/so/rest/exceptions/RestProcessingException.java
+++ b/common/src/main/java/org/onap/so/rest/exceptions/RestProcessingException.java
@@ -26,12 +26,28 @@ package org.onap.so.rest.exceptions;
public class RestProcessingException extends RuntimeException {
private static final long serialVersionUID = 16862313537198441L;
+ private final int statusCode;
public RestProcessingException(final String message) {
super(message);
+ statusCode = 0;
}
public RestProcessingException(final String message, final Throwable cause) {
+ this(message, cause, 0);
+ }
+
+ public RestProcessingException(final String message, final Throwable cause, final int statusCode) {
super(message, cause);
+ this.statusCode = statusCode;
+ }
+
+ /**
+ * Get the status code from the response to the rest request, if available
+ *
+ * @return the status code, or 0 if not available
+ */
+ public int getStatusCode() {
+ return statusCode;
}
}
diff --git a/common/src/main/java/org/onap/so/rest/service/HttpRestServiceProviderImpl.java b/common/src/main/java/org/onap/so/rest/service/HttpRestServiceProviderImpl.java
index a627e82802..b82d73bbbf 100644
--- a/common/src/main/java/org/onap/so/rest/service/HttpRestServiceProviderImpl.java
+++ b/common/src/main/java/org/onap/so/rest/service/HttpRestServiceProviderImpl.java
@@ -23,6 +23,7 @@ package org.onap.so.rest.service;
import com.google.common.base.Optional;
import org.onap.so.configuration.rest.BasicHttpHeadersProvider;
import org.onap.so.configuration.rest.HttpHeadersProvider;
+import org.onap.so.rest.exceptions.HttpResouceNotFoundException;
import org.onap.so.rest.exceptions.InvalidRestRequestException;
import org.onap.so.rest.exceptions.RestProcessingException;
import org.slf4j.Logger;
@@ -32,7 +33,7 @@ import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
-import org.springframework.web.client.HttpClientErrorException;
+import org.springframework.web.client.HttpStatusCodeException;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;
@@ -116,15 +117,18 @@ public class HttpRestServiceProviderImpl implements HttpRestServiceProvider {
try {
return restTemplate.exchange(url, httpMethod, request, clazz);
- } catch (final HttpClientErrorException httpClientErrorException) {
+ } catch (final HttpStatusCodeException httpStatusCodeException) {
final String message = "Unable to invoke HTTP " + httpMethod + " using url: " + url + ", Response: "
- + httpClientErrorException.getRawStatusCode();
- LOGGER.error(message, httpClientErrorException);
- final int rawStatusCode = httpClientErrorException.getRawStatusCode();
- if (rawStatusCode == HttpStatus.BAD_REQUEST.value() || rawStatusCode == HttpStatus.NOT_FOUND.value()) {
+ + httpStatusCodeException.getRawStatusCode();
+ LOGGER.error(message, httpStatusCodeException);
+ final int rawStatusCode = httpStatusCodeException.getRawStatusCode();
+ if (rawStatusCode == HttpStatus.BAD_REQUEST.value()) {
throw new InvalidRestRequestException("No result found for given url: " + url);
+ } else if (rawStatusCode == HttpStatus.NOT_FOUND.value()) {
+ throw new HttpResouceNotFoundException("No result found for given url: " + url);
}
- throw new RestProcessingException("Unable to invoke HTTP " + httpMethod + " using URL: " + url);
+ throw new RestProcessingException("Unable to invoke HTTP " + httpMethod + " using URL: " + url,
+ httpStatusCodeException, rawStatusCode);
} catch (final RestClientException restClientException) {
LOGGER.error("Unable to invoke HTTP POST using url: {}", url, restClientException);
diff --git a/common/src/test/java/org/onap/so/rest/service/HttpRestServiceProviderImplTest.java b/common/src/test/java/org/onap/so/rest/service/HttpRestServiceProviderImplTest.java
index 978c016dec..72bacdf2db 100644
--- a/common/src/test/java/org/onap/so/rest/service/HttpRestServiceProviderImplTest.java
+++ b/common/src/test/java/org/onap/so/rest/service/HttpRestServiceProviderImplTest.java
@@ -33,6 +33,7 @@ import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.onap.so.rest.exceptions.InvalidRestRequestException;
+import org.onap.so.rest.exceptions.HttpResouceNotFoundException;
import org.onap.so.rest.exceptions.RestProcessingException;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
@@ -116,7 +117,7 @@ public class HttpRestServiceProviderImplTest {
}
- @Test(expected = InvalidRestRequestException.class)
+ @Test(expected = HttpResouceNotFoundException.class)
public void test_get_ThrowsInvalidRestRequestExceptionifHttpClientErrorExceptionWithHttpStatusNotFoundHttpStatus() {
assertGetErrorScenario(HttpStatus.NOT_FOUND);
}
@@ -239,7 +240,7 @@ public class HttpRestServiceProviderImplTest {
}
- @Test(expected = InvalidRestRequestException.class)
+ @Test(expected = HttpResouceNotFoundException.class)
public void test_post_ThrowsInvalidRestRequestExceptionifHttpClientErrorExceptionWithHttpStatusNotFoundHttpStatus() {
assertPostErrorScenario(HttpStatus.NOT_FOUND);
}