From 20dcdbc1384a1173d7e63dd666d530908c845a1e Mon Sep 17 00:00:00 2001 From: Brinda Santh Date: Fri, 6 Mar 2020 13:04:37 -0500 Subject: Refactoring BP Code with ErrorCatalog Issue-ID: CCSDK-2124 Signed-off-by: Steve Siani Change-Id: Ief468a56f9e7b3ef86c357965aa7b15f0a4cfa22 --- .../error/catalog/core/ErrorCatalogException.kt | 20 +++++++++++ .../ccsdk/cds/error/catalog/core/ErrorCodes.kt | 6 ++-- .../ccsdk/cds/error/catalog/core/ErrorLibData.kt | 10 +++++- .../error/catalog/core/utils/ErrorCatalogUtils.kt | 41 ++++++++++++++++++++++ 4 files changed, 72 insertions(+), 5 deletions(-) create mode 100644 ms/error-catalog/core/src/main/kotlin/org/onap/ccsdk/cds/error/catalog/core/utils/ErrorCatalogUtils.kt (limited to 'ms/error-catalog/core/src/main') diff --git a/ms/error-catalog/core/src/main/kotlin/org/onap/ccsdk/cds/error/catalog/core/ErrorCatalogException.kt b/ms/error-catalog/core/src/main/kotlin/org/onap/ccsdk/cds/error/catalog/core/ErrorCatalogException.kt index 032feb62c..348b60ceb 100644 --- a/ms/error-catalog/core/src/main/kotlin/org/onap/ccsdk/cds/error/catalog/core/ErrorCatalogException.kt +++ b/ms/error-catalog/core/src/main/kotlin/org/onap/ccsdk/cds/error/catalog/core/ErrorCatalogException.kt @@ -23,6 +23,8 @@ interface ErrorCatalogExceptionFluent { fun action(action: String): T fun http(type: String): T fun grpc(type: String): T + fun convertToHttp(): T + fun convertToGrpc(): T fun payloadMessage(message: String): T fun addErrorPayloadMessage(message: String): T fun addSubError(errorMessage: ErrorMessage): T @@ -78,12 +80,30 @@ open class ErrorCatalogException : RuntimeException { fun updateHttp(type: String): T { this.protocol = ErrorMessageLibConstants.ERROR_CATALOG_PROTOCOL_HTTP + this.name = type this.code = HttpErrorCodes.code(type) return this as T } + fun inverseToHttp(): T { + if (this.protocol != "" && this.protocol == ErrorMessageLibConstants.ERROR_CATALOG_PROTOCOL_GRPC) { + this.protocol = ErrorMessageLibConstants.ERROR_CATALOG_PROTOCOL_HTTP + this.code = HttpErrorCodes.code(this.name) + } + return this as T + } + + fun inverseToGrpc(): T { + if (this.protocol != "" && this.protocol == ErrorMessageLibConstants.ERROR_CATALOG_PROTOCOL_HTTP) { + this.protocol = ErrorMessageLibConstants.ERROR_CATALOG_PROTOCOL_GRPC + this.code = GrpcErrorCodes.code(this.name) + } + return this as T + } + fun updateGrpc(type: String): T { this.protocol = ErrorMessageLibConstants.ERROR_CATALOG_PROTOCOL_GRPC + this.name = type this.code = GrpcErrorCodes.code(type) return this as T } diff --git a/ms/error-catalog/core/src/main/kotlin/org/onap/ccsdk/cds/error/catalog/core/ErrorCodes.kt b/ms/error-catalog/core/src/main/kotlin/org/onap/ccsdk/cds/error/catalog/core/ErrorCodes.kt index 86483e3e9..8023d97c8 100644 --- a/ms/error-catalog/core/src/main/kotlin/org/onap/ccsdk/cds/error/catalog/core/ErrorCodes.kt +++ b/ms/error-catalog/core/src/main/kotlin/org/onap/ccsdk/cds/error/catalog/core/ErrorCodes.kt @@ -54,8 +54,7 @@ object HttpErrorCodes { } fun code(type: String): Int { - // FIXME("Return Default Error Code , If missing") - return store[type]!! + return store[type] ?: store[ErrorCatalogCodes.GENERIC_FAILURE]!! } } @@ -82,7 +81,6 @@ object GrpcErrorCodes { } fun code(type: String): Int { - // FIXME("Return Default Error Code , If missing") - return store[type]!! + return store[type] ?: store[ErrorCatalogCodes.GENERIC_FAILURE]!! } } diff --git a/ms/error-catalog/core/src/main/kotlin/org/onap/ccsdk/cds/error/catalog/core/ErrorLibData.kt b/ms/error-catalog/core/src/main/kotlin/org/onap/ccsdk/cds/error/catalog/core/ErrorLibData.kt index 2c0772e31..4158cfaf8 100644 --- a/ms/error-catalog/core/src/main/kotlin/org/onap/ccsdk/cds/error/catalog/core/ErrorLibData.kt +++ b/ms/error-catalog/core/src/main/kotlin/org/onap/ccsdk/cds/error/catalog/core/ErrorLibData.kt @@ -20,12 +20,15 @@ import com.fasterxml.jackson.annotation.JsonFormat import org.slf4j.event.Level import org.onap.ccsdk.cds.error.catalog.core.ErrorMessageLibConstants.ERROR_CATALOG_DEFAULT_ERROR_CODE import java.time.LocalDateTime +import java.time.ZoneId +import java.util.Date +import kotlin.collections.ArrayList open class ErrorPayload { var code: Int = ERROR_CATALOG_DEFAULT_ERROR_CODE var status: String = "" @get:JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'") - var timestamp: LocalDateTime = LocalDateTime.now() + var timestamp: Date = controllerDate() var message: String = "" var debugMessage: String = "" var logLevel: String = Level.ERROR.name @@ -68,6 +71,11 @@ open class ErrorPayload { this.logLevel == errorPayload.logLevel && this.debugMessage == errorPayload.debugMessage && this.subErrors == errorPayload.subErrors) } + + private fun controllerDate(): Date { + val localDateTime = LocalDateTime.now(ZoneId.systemDefault()) + return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant()) + } } /** diff --git a/ms/error-catalog/core/src/main/kotlin/org/onap/ccsdk/cds/error/catalog/core/utils/ErrorCatalogUtils.kt b/ms/error-catalog/core/src/main/kotlin/org/onap/ccsdk/cds/error/catalog/core/utils/ErrorCatalogUtils.kt new file mode 100644 index 000000000..f13a3604f --- /dev/null +++ b/ms/error-catalog/core/src/main/kotlin/org/onap/ccsdk/cds/error/catalog/core/utils/ErrorCatalogUtils.kt @@ -0,0 +1,41 @@ +/* + * 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.core.utils + +import org.apache.commons.lang3.exception.ExceptionUtils + +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 ExceptionUtils.getRootCause(this) +} + +fun Exception.errorMessageOrDefault(): String { + return this.message ?: "" +} -- cgit 1.2.3-korg