Fix: Run both sonar and clm scans in parallel
[ccsdk/cds.git] / ms / error-catalog / services / src / main / kotlin / org / onap / ccsdk / cds / error / catalog / services / ErrorCatalogExceptionHandler.kt
1 /*
2  *  Copyright © 2020 IBM, Bell Canada.
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.error.catalog.services
18
19 import org.onap.ccsdk.cds.error.catalog.core.ErrorCatalogCodes
20 import org.onap.ccsdk.cds.error.catalog.core.ErrorCatalogException
21 import org.onap.ccsdk.cds.error.catalog.core.ErrorPayload
22 import org.onap.ccsdk.cds.error.catalog.core.HttpErrorCodes
23 import org.onap.ccsdk.cds.error.catalog.core.utils.errorCauseOrDefault
24 import org.onap.ccsdk.cds.error.catalog.core.utils.errorMessageOrDefault
25 import org.springframework.dao.EmptyResultDataAccessException
26 import org.springframework.dao.IncorrectResultSizeDataAccessException
27 import org.springframework.http.ResponseEntity
28 import org.springframework.orm.jpa.JpaObjectRetrievalFailureException
29 import org.springframework.web.bind.annotation.ExceptionHandler
30 import org.springframework.web.server.ServerWebInputException
31
32 abstract class ErrorCatalogExceptionHandler(private val errorCatalogService: ErrorCatalogService) {
33
34     @ExceptionHandler(ErrorCatalogException::class)
35     fun errorCatalogException(e: ErrorCatalogException): ResponseEntity<ErrorPayload> {
36         val errorPayload = errorCatalogService.errorPayload(e)
37         return errorPayload.toResponseEntity()
38     }
39
40     @ExceptionHandler
41     fun errorCatalogException(e: ServerWebInputException): ResponseEntity<ErrorPayload> {
42         val error = ErrorCatalogException(
43             HttpErrorCodes.code(ErrorCatalogCodes.REQUEST_NOT_FOUND),
44             e.errorMessageOrDefault(), e.errorCauseOrDefault()
45         )
46         val errorPayload = ErrorPayload(error.code, error.name, error.errorMessageOrDefault())
47         return errorPayload.toResponseEntity()
48     }
49
50     @ExceptionHandler
51     fun errorCatalogException(e: IncorrectResultSizeDataAccessException): ResponseEntity<ErrorPayload> {
52         val error = ErrorCatalogException(
53             HttpErrorCodes.code(ErrorCatalogCodes.DUPLICATE_DATA),
54             e.errorMessageOrDefault(), e.errorCauseOrDefault()
55         )
56         val errorPayload = ErrorPayload(error.code, error.name, error.errorMessageOrDefault())
57         return errorPayload.toResponseEntity()
58     }
59
60     @ExceptionHandler
61     fun errorCatalogException(e: EmptyResultDataAccessException): ResponseEntity<ErrorPayload> {
62         val error = ErrorCatalogException(
63             HttpErrorCodes.code(ErrorCatalogCodes.RESOURCE_NOT_FOUND),
64             e.errorMessageOrDefault(), e.errorCauseOrDefault()
65         )
66         val errorPayload = ErrorPayload(error.code, error.name, error.errorMessageOrDefault())
67         return errorPayload.toResponseEntity()
68     }
69
70     @ExceptionHandler
71     fun errorCatalogException(e: JpaObjectRetrievalFailureException): ResponseEntity<ErrorPayload> {
72         val error = ErrorCatalogException(
73             HttpErrorCodes.code(ErrorCatalogCodes.RESOURCE_NOT_FOUND),
74             e.errorMessageOrDefault(), e.errorCauseOrDefault()
75         )
76         val errorPayload = ErrorPayload(error.code, error.name, error.errorMessageOrDefault())
77         return errorPayload.toResponseEntity()
78     }
79
80     @ExceptionHandler
81     fun errorCatalogException(e: Exception): ResponseEntity<ErrorPayload> {
82         val error = ErrorCatalogException(
83             HttpErrorCodes.code(ErrorCatalogCodes.GENERIC_FAILURE),
84             e.errorMessageOrDefault(), e.errorCauseOrDefault()
85         )
86         val errorPayload = ErrorPayload(error.code, error.name, error.errorMessageOrDefault())
87         return errorPayload.toResponseEntity()
88     }
89 }