f4dabcded2dd5f011752edb09a33d47ac293ee8a
[ccsdk/cds.git] /
1 /*
2  * Copyright © 2018-2019 Bell Canada Intellectual Property.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.onap.ccsdk.cds.blueprintsprocessor.designer.api
18
19 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintException
20 import org.onap.ccsdk.cds.controllerblueprints.core.data.ErrorCode
21 import org.onap.ccsdk.cds.controllerblueprints.service.common.ErrorMessage
22 import org.slf4j.LoggerFactory
23 import org.springframework.http.HttpStatus
24 import org.springframework.http.ResponseEntity
25 import org.springframework.web.bind.annotation.ExceptionHandler
26 import org.springframework.web.bind.annotation.RestControllerAdvice
27
28 /**
29  * ControllerBlueprintExceptionHandler Purpose: Handle exceptions in controllerBlueprint API and provide the right
30  * HTTP code status
31  *
32  * @author Vinal Patel
33  * @version 1.0
34  */
35 @RestControllerAdvice("org.onap.ccsdk.cds.controllerblueprints")
36 open class ControllerBlueprintExceptionHandler {
37
38     companion object ControllerBlueprintExceptionHandler {
39         val LOG = LoggerFactory.getLogger(ControllerBlueprintExceptionHandler::class.java)
40     }
41
42     @ExceptionHandler
43     fun ControllerBlueprintExceptionHandler(e: BluePrintException): ResponseEntity<ErrorMessage> {
44         val errorCode = ErrorCode.valueOf(e.code)
45         val errorMessage = ErrorMessage(errorCode?.message(e.message!!), errorCode?.value, "ControllerBluePrint_Error_Message")
46         LOG.error("Error: $errorCode ${e.message}")
47         return ResponseEntity(errorMessage, HttpStatus.resolve(errorCode!!.httpCode))
48     }
49
50     @ExceptionHandler
51     fun ControllerBlueprintExceptionHandler(e: Exception): ResponseEntity<ErrorMessage> {
52         val errorCode = ErrorCode.GENERIC_FAILURE
53         val errorMessage = ErrorMessage(errorCode?.message(e.message!!), errorCode?.value, "ControllerBluePrint_Error_Message")
54         LOG.error("Error: $errorCode ${e.message}")
55         return ResponseEntity(errorMessage, HttpStatus.resolve(errorCode!!.httpCode))
56     }
57 }