Fixnig file import in script, import and template&mapping.
[ccsdk/cds.git] / ms / error-catalog / core / src / main / kotlin / org / onap / ccsdk / cds / error / catalog / core / ErrorCatalogException.kt
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 payloadMessage(message: String): T
27     fun addErrorPayloadMessage(message: String): T
28     fun addSubError(errorMessage: ErrorMessage): T
29 }
30
31 open class ErrorCatalogException : RuntimeException {
32     var code: Int = -1
33     var domain: String = ""
34     var name: String = ErrorCatalogCodes.GENERIC_FAILURE
35     var action: String = ""
36     var errorPayload: ErrorPayload? = null
37     var protocol: String = ""
38     var errorPayloadMessages: MutableList<String>? = null
39
40     val messageSeparator = "${System.lineSeparator()} -> "
41
42     constructor(message: String, cause: Throwable) : super(message, cause)
43     constructor(message: String) : super(message)
44     constructor(cause: Throwable) : super(cause)
45     constructor(cause: Throwable, message: String, vararg args: Any?) : super(format(message, *args), cause)
46
47     constructor(code: Int, cause: Throwable) : super(cause) {
48         this.code = code
49     }
50
51     constructor(code: Int, message: String) : super(message) {
52         this.code = code
53     }
54
55     constructor(code: Int, message: String, cause: Throwable) : super(message, cause) {
56         this.code = code
57     }
58
59     constructor(code: Int, cause: Throwable, message: String, vararg args: Any?) :
60         super(String.format(message, *args), cause) {
61         this.code = code
62     }
63
64     open fun <T : ErrorCatalogException> updateCode(code: Int): T {
65         this.code = code
66         return this as T
67     }
68
69     open fun <T : ErrorCatalogException> updateDomain(domain: String): T {
70         this.domain = domain
71         return this as T
72     }
73
74     open fun <T : ErrorCatalogException> updateAction(action: String): T {
75         this.action = action
76         return this as T
77     }
78
79     fun <T : ErrorCatalogException> updateHttp(type: String): T {
80         this.protocol = ErrorMessageLibConstants.ERROR_CATALOG_PROTOCOL_HTTP
81         this.code = HttpErrorCodes.code(type)
82         return this as T
83     }
84
85     fun <T : ErrorCatalogException> updateGrpc(type: String): T {
86         this.protocol = ErrorMessageLibConstants.ERROR_CATALOG_PROTOCOL_GRPC
87         this.code = GrpcErrorCodes.code(type)
88         return this as T
89     }
90
91     fun <T : ErrorCatalogException> updatePayloadMessage(message: String): T {
92         if (this.errorPayloadMessages == null) this.errorPayloadMessages = arrayListOf()
93         this.errorPayloadMessages!!.add(message)
94         return this as T
95     }
96
97     fun <T : ErrorCatalogException> updateErrorPayloadMessage(message: String): T {
98         if (errorPayload == null) {
99             errorPayload = ErrorPayload()
100         }
101         errorPayload!!.message = "${errorPayload!!.message} $messageSeparator $message"
102         return this as T
103     }
104
105     fun <T : ErrorCatalogException> updateSubError(errorMessage: ErrorMessage): T {
106         if (errorPayload == null) {
107             errorPayload = ErrorPayload()
108         }
109         errorPayload!!.subErrors.add(errorMessage)
110         return this as T
111     }
112 }