ServiceExceptions.py 1.3 KB
Newer Older
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
import grpc
from typing import Iterable

class ServiceException(Exception):
    def __init__(self, code : grpc.StatusCode, details : str, extra_details : Iterable[str] = []) -> None:
        self.code = code
        self.details = '; '.join(map(str, [details] + extra_details))
        super().__init__(self.details)

class NotFoundException(ServiceException):
    def __init__(self, object_name : str, object_uuid: str, extra_details : Iterable[str] = []) -> None:
        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):
    def __init__(self, object_name : str, object_uuid: str, extra_details : Iterable[str] = None) -> None:
        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):
    def __init__(self, argument_name : str, argument_value: str, extra_details : Iterable[str] = None) -> None:
        details = '{:s}({:s}) is invalid'.format(str(argument_name), str(argument_value))
        super().__init__(grpc.StatusCode.INVALID_ARGUMENT, details, extra_details=extra_details)