2 * Copyright © 2020 IBM, Bell Canada.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.onap.ccsdk.cds.error.catalog.services
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
32 abstract class ErrorCatalogExceptionHandler(private val errorCatalogService: ErrorCatalogService) {
34 @ExceptionHandler(ErrorCatalogException::class)
35 fun errorCatalogException(e: ErrorCatalogException): ResponseEntity<ErrorPayload> {
36 val errorPayload = errorCatalogService.errorPayload(e)
37 return errorPayload.toResponseEntity()
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()
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()
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()
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()
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()