diff options
author | Steve Smokowski <ss835w@att.com> | 2019-10-09 11:47:12 +0000 |
---|---|---|
committer | Gerrit Code Review <gerrit@onap.org> | 2019-10-09 11:47:12 +0000 |
commit | 52ad94fa9c73e3edb96abf4dc99fa32c892ded5e (patch) | |
tree | 201dbcd8af17d5c4244dfe3c131f3f7ba0f9d67f /common/src/main | |
parent | 9c55b74f92bf4d4f017504f917b0bce617a523c8 (diff) | |
parent | fbf6300274558ffe323bf66bb5b23c72c350d3ee (diff) |
Merge "Improved error handling"
Diffstat (limited to 'common/src/main')
3 files changed, 58 insertions, 7 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); |