258209f71381035dfeb03a9f0d76d7881af10a6d
[ccsdk/cds.git] /
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(HttpErrorCodes.code(ErrorCatalogCodes.REQUEST_NOT_FOUND),
43                 e.errorMessageOrDefault(), e.errorCauseOrDefault())
44         val errorPayload = ErrorPayload(error.code, error.name, error.errorMessageOrDefault())
45         return errorPayload.toResponseEntity()
46     }
47
48     @ExceptionHandler
49     fun errorCatalogException(e: IncorrectResultSizeDataAccessException): ResponseEntity<ErrorPayload> {
50         val error = ErrorCatalogException(HttpErrorCodes.code(ErrorCatalogCodes.DUPLICATE_DATA),
51                 e.errorMessageOrDefault(), e.errorCauseOrDefault())
52         val errorPayload = ErrorPayload(error.code, error.name, error.errorMessageOrDefault())
53         return errorPayload.toResponseEntity()
54     }
55
56     @ExceptionHandler
57     fun errorCatalogException(e: EmptyResultDataAccessException): ResponseEntity<ErrorPayload> {
58         val error = ErrorCatalogException(HttpErrorCodes.code(ErrorCatalogCodes.RESOURCE_NOT_FOUND),
59                 e.errorMessageOrDefault(), e.errorCauseOrDefault())
60         val errorPayload = ErrorPayload(error.code, error.name, error.errorMessageOrDefault())
61         return errorPayload.toResponseEntity()
62     }
63
64     @ExceptionHandler
65     fun errorCatalogException(e: JpaObjectRetrievalFailureException): ResponseEntity<ErrorPayload> {
66         val error = ErrorCatalogException(HttpErrorCodes.code(ErrorCatalogCodes.RESOURCE_NOT_FOUND),
67                 e.errorMessageOrDefault(), e.errorCauseOrDefault())
68         val errorPayload = ErrorPayload(error.code, error.name, error.errorMessageOrDefault())
69         return errorPayload.toResponseEntity()
70     }
71
72     @ExceptionHandler
73     fun errorCatalogException(e: Exception): ResponseEntity<ErrorPayload> {
74         val error = ErrorCatalogException(HttpErrorCodes.code(ErrorCatalogCodes.GENERIC_FAILURE),
75                 e.errorMessageOrDefault(), e.errorCauseOrDefault())
76         val errorPayload = ErrorPayload(error.code, error.name, error.errorMessageOrDefault())
77         return errorPayload.toResponseEntity()
78     }
79 }