ServiceExceptions.py 3.19 KB
Newer Older
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
# Copyright 2022-2023 ETSI TeraFlowSDN - TFS OSG (https://tfs.etsi.org/)
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
import grpc
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
from typing import Iterable, List, Tuple, Union
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed

class ServiceException(Exception):
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
    def __init__(
        self, code : grpc.StatusCode, details : str, extra_details : Union[str, Iterable[str]] = []
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
    ) -> None:
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
        self.code = code
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
        if isinstance(extra_details, str): extra_details = [extra_details]
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
        self.details = '; '.join([str(item) for item in ([details] + extra_details)])
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
        super().__init__(self.details)

class NotFoundException(ServiceException):
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
    def __init__(
        self, object_name : str, object_uuid: str, extra_details : Union[str, Iterable[str]] = []
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
    ) -> None:
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
        details = '{:s}({:s}) not found'.format(str(object_name), str(object_uuid))
        super().__init__(grpc.StatusCode.NOT_FOUND, details, extra_details=extra_details)

class AlreadyExistsException(ServiceException):
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
    def __init__(
        self, object_name : str, object_uuid: str, extra_details : Union[str, Iterable[str]] = None
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
    ) -> None:
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
        details = '{:s}({:s}) already exists'.format(str(object_name), str(object_uuid))
        super().__init__(grpc.StatusCode.ALREADY_EXISTS, details, extra_details=extra_details)

class InvalidArgumentException(ServiceException):
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
    def __init__(
        self, argument_name : str, argument_value: str, extra_details : Union[str, Iterable[str]] = None
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
    ) -> None:
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
        details = '{:s}({:s}) is invalid'.format(str(argument_name), str(argument_value))
        super().__init__(grpc.StatusCode.INVALID_ARGUMENT, details, extra_details=extra_details)
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
class InvalidArgumentsException(ServiceException):
    def __init__(
        self, arguments : List[Tuple[str, str]], extra_details : Union[str, Iterable[str]] = None
    ) -> None:
        str_arguments = ', '.join(['{:s}({:s})'.format(name, value) for name,value in arguments])
        details = 'Arguments {:s} are invalid'.format(str_arguments)
        super().__init__(grpc.StatusCode.INVALID_ARGUMENT, details, extra_details=extra_details)

class OperationFailedException(ServiceException):
    def __init__(
        self, operation : str, extra_details : Union[str, Iterable[str]] = None
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
    ) -> None:
        details = 'Operation({:s}) failed'.format(str(operation))
        super().__init__(grpc.StatusCode.INTERNAL, details, extra_details=extra_details)
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed

class NotImplementedException(ServiceException):
    def __init__(
        self, operation : str, extra_details : Union[str, Iterable[str]] = None
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
    ) -> None:
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
        details = 'Operation({:s}) not implemented'.format(str(operation))
        super().__init__(grpc.StatusCode.UNIMPLEMENTED, details, extra_details=extra_details)