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(
43 HttpErrorCodes.code(ErrorCatalogCodes.REQUEST_NOT_FOUND),
44 e.errorMessageOrDefault(), e.errorCauseOrDefault()
46 val errorPayload = ErrorPayload(error.code, error.name, error.errorMessageOrDefault())
47 return errorPayload.toResponseEntity()
51 fun errorCatalogException(e: IncorrectResultSizeDataAccessException): ResponseEntity<ErrorPayload> {
52 val error = ErrorCatalogException(
53 HttpErrorCodes.code(ErrorCatalogCodes.DUPLICATE_DATA),
54 e.errorMessageOrDefault(), e.errorCauseOrDefault()
56 val errorPayload = ErrorPayload(error.code, error.name, error.errorMessageOrDefault())
57 return errorPayload.toResponseEntity()
61 fun errorCatalogException(e: EmptyResultDataAccessException): ResponseEntity<ErrorPayload> {
62 val error = ErrorCatalogException(
63 HttpErrorCodes.code(ErrorCatalogCodes.RESOURCE_NOT_FOUND),
64 e.errorMessageOrDefault(), e.errorCauseOrDefault()
66 val errorPayload = ErrorPayload(error.code, error.name, error.errorMessageOrDefault())
67 return errorPayload.toResponseEntity()
71 fun errorCatalogException(e: JpaObjectRetrievalFailureException): ResponseEntity<ErrorPayload> {
72 val error = ErrorCatalogException(
73 HttpErrorCodes.code(ErrorCatalogCodes.RESOURCE_NOT_FOUND),
74 e.errorMessageOrDefault(), e.errorCauseOrDefault()
76 val errorPayload = ErrorPayload(error.code, error.name, error.errorMessageOrDefault())
77 return errorPayload.toResponseEntity()
81 fun errorCatalogException(e: Exception): ResponseEntity<ErrorPayload> {
82 val error = ErrorCatalogException(
83 HttpErrorCodes.code(ErrorCatalogCodes.GENERIC_FAILURE),
84 e.errorMessageOrDefault(), e.errorCauseOrDefault()
86 val errorPayload = ErrorPayload(error.code, error.name, error.errorMessageOrDefault())
87 return errorPayload.toResponseEntity()