diff options
author | Brinda Santh <bs2796@att.com> | 2020-03-06 13:04:37 -0500 |
---|---|---|
committer | Dan Timoney <dtimoney@att.com> | 2020-04-08 12:19:51 -0400 |
commit | 4384dfcb4c2147f9c2ae0142a4f01dd94c347c50 (patch) | |
tree | 57c5eafb81b00c6fc38ef2eb7eda18db7cce392b /ms/error-catalog/services/src | |
parent | 01d045f109c24749c9ed46f4008f496e4bf62bcb (diff) |
Refactoring BP Code with ErrorCatalog
Issue-ID: CCSDK-2124
Signed-off-by: Steve Siani <alphonse.steve.siani.djissitchi@ibm.com>
Change-Id: Ief468a56f9e7b3ef86c357965aa7b15f0a4cfa22
Signed-off-by: Dan Timoney <dtimoney@att.com>
Diffstat (limited to 'ms/error-catalog/services/src')
3 files changed, 60 insertions, 43 deletions
diff --git a/ms/error-catalog/services/src/main/kotlin/org/onap/ccsdk/cds/error/catalog/services/ErrorCatalogExceptionHandler.kt b/ms/error-catalog/services/src/main/kotlin/org/onap/ccsdk/cds/error/catalog/services/ErrorCatalogExceptionHandler.kt index 88e2f4522..258209f71 100644 --- a/ms/error-catalog/services/src/main/kotlin/org/onap/ccsdk/cds/error/catalog/services/ErrorCatalogExceptionHandler.kt +++ b/ms/error-catalog/services/src/main/kotlin/org/onap/ccsdk/cds/error/catalog/services/ErrorCatalogExceptionHandler.kt @@ -16,10 +16,18 @@ package org.onap.ccsdk.cds.error.catalog.services +import org.onap.ccsdk.cds.error.catalog.core.ErrorCatalogCodes import org.onap.ccsdk.cds.error.catalog.core.ErrorCatalogException import org.onap.ccsdk.cds.error.catalog.core.ErrorPayload +import org.onap.ccsdk.cds.error.catalog.core.HttpErrorCodes +import org.onap.ccsdk.cds.error.catalog.core.utils.errorCauseOrDefault +import org.onap.ccsdk.cds.error.catalog.core.utils.errorMessageOrDefault +import org.springframework.dao.EmptyResultDataAccessException +import org.springframework.dao.IncorrectResultSizeDataAccessException import org.springframework.http.ResponseEntity +import org.springframework.orm.jpa.JpaObjectRetrievalFailureException import org.springframework.web.bind.annotation.ExceptionHandler +import org.springframework.web.server.ServerWebInputException abstract class ErrorCatalogExceptionHandler(private val errorCatalogService: ErrorCatalogService) { @@ -28,4 +36,44 @@ abstract class ErrorCatalogExceptionHandler(private val errorCatalogService: Err val errorPayload = errorCatalogService.errorPayload(e) return errorPayload.toResponseEntity() } + + @ExceptionHandler + fun errorCatalogException(e: ServerWebInputException): ResponseEntity<ErrorPayload> { + val error = ErrorCatalogException(HttpErrorCodes.code(ErrorCatalogCodes.REQUEST_NOT_FOUND), + e.errorMessageOrDefault(), e.errorCauseOrDefault()) + val errorPayload = ErrorPayload(error.code, error.name, error.errorMessageOrDefault()) + return errorPayload.toResponseEntity() + } + + @ExceptionHandler + fun errorCatalogException(e: IncorrectResultSizeDataAccessException): ResponseEntity<ErrorPayload> { + val error = ErrorCatalogException(HttpErrorCodes.code(ErrorCatalogCodes.DUPLICATE_DATA), + e.errorMessageOrDefault(), e.errorCauseOrDefault()) + val errorPayload = ErrorPayload(error.code, error.name, error.errorMessageOrDefault()) + return errorPayload.toResponseEntity() + } + + @ExceptionHandler + fun errorCatalogException(e: EmptyResultDataAccessException): ResponseEntity<ErrorPayload> { + val error = ErrorCatalogException(HttpErrorCodes.code(ErrorCatalogCodes.RESOURCE_NOT_FOUND), + e.errorMessageOrDefault(), e.errorCauseOrDefault()) + val errorPayload = ErrorPayload(error.code, error.name, error.errorMessageOrDefault()) + return errorPayload.toResponseEntity() + } + + @ExceptionHandler + fun errorCatalogException(e: JpaObjectRetrievalFailureException): ResponseEntity<ErrorPayload> { + val error = ErrorCatalogException(HttpErrorCodes.code(ErrorCatalogCodes.RESOURCE_NOT_FOUND), + e.errorMessageOrDefault(), e.errorCauseOrDefault()) + val errorPayload = ErrorPayload(error.code, error.name, error.errorMessageOrDefault()) + return errorPayload.toResponseEntity() + } + + @ExceptionHandler + fun errorCatalogException(e: Exception): ResponseEntity<ErrorPayload> { + val error = ErrorCatalogException(HttpErrorCodes.code(ErrorCatalogCodes.GENERIC_FAILURE), + e.errorMessageOrDefault(), e.errorCauseOrDefault()) + val errorPayload = ErrorPayload(error.code, error.name, error.errorMessageOrDefault()) + return errorPayload.toResponseEntity() + } } diff --git a/ms/error-catalog/services/src/main/kotlin/org/onap/ccsdk/cds/error/catalog/services/ErrorCatalogService.kt b/ms/error-catalog/services/src/main/kotlin/org/onap/ccsdk/cds/error/catalog/services/ErrorCatalogService.kt index 91f817133..21fd51b2f 100644 --- a/ms/error-catalog/services/src/main/kotlin/org/onap/ccsdk/cds/error/catalog/services/ErrorCatalogService.kt +++ b/ms/error-catalog/services/src/main/kotlin/org/onap/ccsdk/cds/error/catalog/services/ErrorCatalogService.kt @@ -24,7 +24,8 @@ import org.onap.ccsdk.cds.error.catalog.core.ErrorMessageLibConstants import org.onap.ccsdk.cds.error.catalog.core.ErrorPayload import org.onap.ccsdk.cds.error.catalog.core.GrpcErrorCodes import org.onap.ccsdk.cds.error.catalog.core.HttpErrorCodes -import org.onap.ccsdk.cds.error.catalog.services.utils.ErrorCatalogUtils +import org.onap.ccsdk.cds.error.catalog.core.utils.ErrorCatalogUtils +import org.apache.commons.lang3.exception.ExceptionUtils import org.springframework.boot.autoconfigure.condition.ConditionalOnBean import org.springframework.stereotype.Service import javax.annotation.PostConstruct @@ -40,10 +41,17 @@ open class ErrorCatalogService(private var errorCatalogLoadService: ErrorCatalog fun errorPayload(errorCatalogException: ErrorCatalogException): ErrorPayload { val errorCatalog = getErrorCatalog(errorCatalogException) - val errorPayload = ErrorPayload(errorCatalog.code, errorCatalog.errorId, errorCatalog.getMessage()) - errorPayload.subErrors.addAll(errorCatalogException.errorPayload!!.subErrors) + val errorPayload: ErrorPayload + if (errorCatalogException.errorPayload == null) { + errorPayload = ErrorPayload(errorCatalog.code, errorCatalog.errorId, errorCatalog.getMessage()) + } else { + errorPayload = errorCatalogException.errorPayload!! + errorPayload.code = errorCatalog.code + errorPayload.message = errorCatalog.getMessage() + errorPayload.status = errorCatalog.errorId + } if (errorCatalogException.cause != null) { - errorPayload.debugMessage = errorCatalogException.cause!!.printStackTrace().toString() + errorPayload.debugMessage = ExceptionUtils.getStackTrace(errorCatalogException.cause) } return errorPayload } diff --git a/ms/error-catalog/services/src/main/kotlin/org/onap/ccsdk/cds/error/catalog/services/utils/ErrorCatalogUtils.kt b/ms/error-catalog/services/src/main/kotlin/org/onap/ccsdk/cds/error/catalog/services/utils/ErrorCatalogUtils.kt deleted file mode 100644 index 967d3560c..000000000 --- a/ms/error-catalog/services/src/main/kotlin/org/onap/ccsdk/cds/error/catalog/services/utils/ErrorCatalogUtils.kt +++ /dev/null @@ -1,39 +0,0 @@ -/* - * Copyright © 2020 IBM, Bell Canada. - * - * 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. - */ -package org.onap.ccsdk.cds.error.catalog.services.utils - -object ErrorCatalogUtils { - private const val REGEX_PATTERN = "^cause=(.*),action=(.*)" - private val regex = REGEX_PATTERN.toRegex() - - fun readErrorCauseFromMessage(message: String): String { - val matchResults = regex.matchEntire(message) - return matchResults!!.groupValues[1] - } - - fun readErrorActionFromMessage(message: String): String { - val matchResults = regex.matchEntire(message) - return matchResults!!.groupValues[2] - } -} - -fun Exception.errorCauseOrDefault(): Throwable { - return this.cause ?: Throwable() -} - -fun Exception.errorMessageOrDefault(): String { - return this.message ?: "" -} |