2 * Copyright © 2020 IBM, Bell Canada.
3 * Modifications Copyright © 2019-2020 AT&T Intellectual Property.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
18 package org.onap.ccsdk.cds.error.catalog.core
20 interface ErrorCatalogExceptionFluent<T> {
21 fun code(code: Int): T
22 fun domain(domain: String): T
23 fun action(action: String): T
24 fun http(type: String): T
25 fun grpc(type: String): T
26 fun convertToHttp(): T
27 fun convertToGrpc(): T
28 fun payloadMessage(message: String): T
29 fun addErrorPayloadMessage(message: String): T
30 fun addSubError(errorMessage: ErrorMessage): T
33 open class ErrorCatalogException : RuntimeException {
35 var domain: String = ""
36 var name: String = ErrorCatalogCodes.GENERIC_FAILURE
37 var action: String = ""
38 var errorPayload: ErrorPayload? = null
39 var protocol: String = ""
40 var errorPayloadMessages: MutableList<String>? = null
42 val messageSeparator = "${System.lineSeparator()} -> "
44 constructor(message: String, cause: Throwable) : super(message, cause)
45 constructor(message: String) : super(message)
46 constructor(cause: Throwable) : super(cause)
47 constructor(cause: Throwable, message: String, vararg args: Any?) : super(format(message, *args), cause)
49 constructor(code: Int, cause: Throwable) : super(cause) {
53 constructor(code: Int, message: String) : super(message) {
57 constructor(code: Int, message: String, cause: Throwable) : super(message, cause) {
61 constructor(code: Int, cause: Throwable, message: String, vararg args: Any?) :
62 super(String.format(message, *args), cause) {
66 open fun <T : ErrorCatalogException> updateCode(code: Int): T {
71 open fun <T : ErrorCatalogException> updateDomain(domain: String): T {
76 open fun <T : ErrorCatalogException> updateAction(action: String): T {
81 fun <T : ErrorCatalogException> updateHttp(type: String): T {
82 this.protocol = ErrorMessageLibConstants.ERROR_CATALOG_PROTOCOL_HTTP
84 this.code = HttpErrorCodes.code(type)
88 fun <T : ErrorCatalogException> inverseToHttp(): T {
89 if (this.protocol != "" && this.protocol == ErrorMessageLibConstants.ERROR_CATALOG_PROTOCOL_GRPC) {
90 this.protocol = ErrorMessageLibConstants.ERROR_CATALOG_PROTOCOL_HTTP
91 this.code = HttpErrorCodes.code(this.name)
96 fun <T : ErrorCatalogException> inverseToGrpc(): T {
97 if (this.protocol != "" && this.protocol == ErrorMessageLibConstants.ERROR_CATALOG_PROTOCOL_HTTP) {
98 this.protocol = ErrorMessageLibConstants.ERROR_CATALOG_PROTOCOL_GRPC
99 this.code = GrpcErrorCodes.code(this.name)
104 fun <T : ErrorCatalogException> updateGrpc(type: String): T {
105 this.protocol = ErrorMessageLibConstants.ERROR_CATALOG_PROTOCOL_GRPC
107 this.code = GrpcErrorCodes.code(type)
111 fun <T : ErrorCatalogException> updatePayloadMessage(message: String): T {
112 if (this.errorPayloadMessages == null) this.errorPayloadMessages = arrayListOf()
113 this.errorPayloadMessages!!.add(message)
117 fun <T : ErrorCatalogException> updateErrorPayloadMessage(message: String): T {
118 if (errorPayload == null) {
119 errorPayload = ErrorPayload()
121 errorPayload!!.message = "${errorPayload!!.message} $messageSeparator $message"
125 fun <T : ErrorCatalogException> updateSubError(errorMessage: ErrorMessage): T {
126 if (errorPayload == null) {
127 errorPayload = ErrorPayload()
129 errorPayload!!.subErrors.add(errorMessage)