348b60ceb0b61aa53d98a74013e885f0657dba54
[ccsdk/cds.git] /
1 /*
2  *  Copyright © 2020 IBM, Bell Canada.
3  *  Modifications Copyright © 2019-2020 AT&T Intellectual Property.
4  *
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  */
17
18 package org.onap.ccsdk.cds.error.catalog.core
19
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
31 }
32
33 open class ErrorCatalogException : RuntimeException {
34     var code: Int = -1
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
41
42     val messageSeparator = "${System.lineSeparator()} -> "
43
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)
48
49     constructor(code: Int, cause: Throwable) : super(cause) {
50         this.code = code
51     }
52
53     constructor(code: Int, message: String) : super(message) {
54         this.code = code
55     }
56
57     constructor(code: Int, message: String, cause: Throwable) : super(message, cause) {
58         this.code = code
59     }
60
61     constructor(code: Int, cause: Throwable, message: String, vararg args: Any?) :
62         super(String.format(message, *args), cause) {
63         this.code = code
64     }
65
66     open fun <T : ErrorCatalogException> updateCode(code: Int): T {
67         this.code = code
68         return this as T
69     }
70
71     open fun <T : ErrorCatalogException> updateDomain(domain: String): T {
72         this.domain = domain
73         return this as T
74     }
75
76     open fun <T : ErrorCatalogException> updateAction(action: String): T {
77         this.action = action
78         return this as T
79     }
80
81     fun <T : ErrorCatalogException> updateHttp(type: String): T {
82         this.protocol = ErrorMessageLibConstants.ERROR_CATALOG_PROTOCOL_HTTP
83         this.name = type
84         this.code = HttpErrorCodes.code(type)
85         return this as T
86     }
87
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)
92         }
93         return this as T
94     }
95
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)
100         }
101         return this as T
102     }
103
104     fun <T : ErrorCatalogException> updateGrpc(type: String): T {
105         this.protocol = ErrorMessageLibConstants.ERROR_CATALOG_PROTOCOL_GRPC
106         this.name = type
107         this.code = GrpcErrorCodes.code(type)
108         return this as T
109     }
110
111     fun <T : ErrorCatalogException> updatePayloadMessage(message: String): T {
112         if (this.errorPayloadMessages == null) this.errorPayloadMessages = arrayListOf()
113         this.errorPayloadMessages!!.add(message)
114         return this as T
115     }
116
117     fun <T : ErrorCatalogException> updateErrorPayloadMessage(message: String): T {
118         if (errorPayload == null) {
119             errorPayload = ErrorPayload()
120         }
121         errorPayload!!.message = "${errorPayload!!.message} $messageSeparator $message"
122         return this as T
123     }
124
125     fun <T : ErrorCatalogException> updateSubError(errorMessage: ErrorMessage): T {
126         if (errorPayload == null) {
127             errorPayload = ErrorPayload()
128         }
129         errorPayload!!.subErrors.add(errorMessage)
130         return this as T
131     }
132 }