acb158d896fd11c589389a456b298a63311fd51f
[ccsdk/cds.git] / ms / blueprintsprocessor / modules / blueprints / blueprint-core / src / main / kotlin / org / onap / ccsdk / cds / controllerblueprints / core / BluePrintProcessorException.kt
1 /*
2  * Copyright © 2017-2018 AT&T Intellectual Property.
3  * Modifications Copyright © 2018 - 2020 IBM, Bell Canada.
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.controllerblueprints.core
19
20 import org.onap.ccsdk.cds.error.catalog.core.ErrorCatalogException
21 import org.onap.ccsdk.cds.error.catalog.core.ErrorCatalogExceptionFluent
22 import org.onap.ccsdk.cds.error.catalog.core.ErrorMessage
23
24 /**
25  *
26  *
27  * @author Brinda Santh
28  */
29 open class BluePrintProcessorException : ErrorCatalogException, ErrorCatalogExceptionFluent<BluePrintProcessorException> {
30
31     constructor(message: String, cause: Throwable) : super(message, cause)
32     constructor(message: String) : super(message)
33     constructor(cause: Throwable) : super(cause)
34     constructor(cause: Throwable, message: String, vararg args: Any?) : super(cause, message, args)
35     constructor(code: Int, cause: Throwable) : super(code, cause)
36     constructor(code: Int, message: String) : super(code, message)
37     constructor(code: Int, message: String, cause: Throwable) : super(code, message, cause)
38
39     override fun code(code: Int): BluePrintProcessorException {
40         return this.updateCode(code)
41     }
42
43     override fun domain(domain: String): BluePrintProcessorException {
44         return this.updateDomain(domain)
45     }
46
47     override fun action(action: String): BluePrintProcessorException {
48         return this.updateAction(action)
49     }
50
51     override fun http(type: String): BluePrintProcessorException {
52         return this.updateHttp(type)
53     }
54
55     override fun grpc(type: String): BluePrintProcessorException {
56         return this.updateGrpc(type)
57     }
58
59     override fun payloadMessage(message: String): BluePrintProcessorException {
60         return this.updatePayloadMessage(message)
61     }
62
63     override fun addErrorPayloadMessage(message: String): BluePrintProcessorException {
64         return this.updateErrorPayloadMessage(message)
65     }
66
67     override fun addSubError(errorMessage: ErrorMessage): BluePrintProcessorException {
68         return this.updateSubError(errorMessage)
69     }
70 }
71
72 class BluePrintRetryException : RuntimeException {
73     constructor(message: String, cause: Throwable) : super(message, cause)
74     constructor(message: String) : super(message)
75     constructor(cause: Throwable) : super(cause)
76     constructor(cause: Throwable, message: String, vararg args: Any?) : super(format(message, *args), cause)
77 }
78
79 /** Extension Functions */
80
81 fun processorException(message: String): BluePrintProcessorException {
82     return BluePrintProcessorException(message)
83 }
84
85 fun processorException(code: Int, message: String): BluePrintProcessorException {
86     return processorException(message).code(code)
87 }
88
89 fun httpProcessorException(type: String, message: String): BluePrintProcessorException {
90     return processorException(message).http(type)
91 }
92
93 fun grpcProcessorException(type: String, message: String): BluePrintProcessorException {
94     return processorException(message).grpc(type)
95 }
96
97 fun httpProcessorException(type: String, domain: String, message: String): BluePrintProcessorException {
98     val bluePrintProcessorException = processorException(message).http(type)
99     return bluePrintProcessorException.addDomainAndErrorMessage(domain, message)
100 }
101
102 fun grpcProcessorException(type: String, domain: String, message: String): BluePrintProcessorException {
103     val bluePrintProcessorException = processorException(message).grpc(type)
104     return bluePrintProcessorException.addDomainAndErrorMessage(domain, message)
105 }
106
107 fun httpProcessorException(type: String, domain: String, message: String, cause: Throwable):
108         BluePrintProcessorException {
109     val bluePrintProcessorException = processorException(message).http(type)
110     return bluePrintProcessorException.addDomainAndErrorMessage(domain, message, cause)
111 }
112
113 fun grpcProcessorException(type: String, domain: String, message: String, cause: Throwable):
114         BluePrintProcessorException {
115     val bluePrintProcessorException = processorException(message).grpc(type)
116     return bluePrintProcessorException.addDomainAndErrorMessage(domain, message, cause)
117 }
118
119 fun BluePrintProcessorException.updateErrorMessage(domain: String, message: String, cause: Throwable):
120         BluePrintProcessorException {
121     return this.addDomainAndErrorMessage(domain, message, cause).domain(domain)
122             .addErrorPayloadMessage(message)
123             .payloadMessage(message)
124 }
125
126 fun BluePrintProcessorException.updateErrorMessage(domain: String, message: String): BluePrintProcessorException {
127     return this.addDomainAndErrorMessage(domain, message).domain(domain)
128             .addErrorPayloadMessage(message)
129             .payloadMessage(message)
130 }
131
132 private fun BluePrintProcessorException.addDomainAndErrorMessage(
133     domain: String,
134     message: String,
135     cause: Throwable = Throwable()
136 ): BluePrintProcessorException {
137     return this.addSubError(ErrorMessage(domain, message, cause.message
138             ?: "")).domain(domain)
139 }