Source code for nova_api.exceptions

"""
Custom exceptions for NovaAPI
"""
from dataclasses import dataclass, field
from typing import Optional


[docs]@dataclass class NovaAPIException(Exception): """Base class for API error reporting This class allows for an easier error handling on API's. \ When use_dao captures one error that subclasses this class, \ it reads through the fields to fill the error response to be \ returned to the user. It is also possible to add a debug \ message which is only sent if DEBUG is set to True. :param status_code: The integer HTTP status code to send in the \ response. Defaults to 500. :param message: The message to send in the message field of the \ response. Defaults to Error. :param error_code: The error code to send in the data object. This \ may be used to help debugging in production environments. Defaults \ to the status code. :param debug: The debug message to send in the data object if \ DEBUG is True. Defaults to "No debug information". """ status_code: int = 500 message: str = "Error" error_code: Optional[int] = None debug: str = "No debug information" def __post_init__(self): if self.error_code is None: self.error_code = self.status_code
[docs]@dataclass class NotEntityException(NovaAPIException): """ Argument is not an Entity """ status_code: int = field(default=400, init=False) message: str = field(default="Argument is not an Entity", init=False)
[docs]@dataclass class InvalidFiltersException(NovaAPIException): """ Filters is not a dictionary """ status_code: int = field(default=400, init=False) message: str = field(default="Filters are not valid", init=False)
[docs]@dataclass class InvalidIDTypeException(NovaAPIException): """ ID is not a string """ status_code: int = field(default=400, init=False) message: str = field(default="ID type is not string", init=False)
[docs]@dataclass class InvalidIDException(NovaAPIException): """ ID is not a valid UUID v4 """ status_code: int = field(default=400, init=False) message: str = field(default="ID is not a valid UUID v4", init=False)
[docs]@dataclass class DuplicateEntityException(NovaAPIException): """ Entity UUID already exists on database. """ status_code: int = field(default=409, init=False) message: str = field(default="Entity already exists in database", init=False)
[docs]@dataclass class EntityNotFoundException(NovaAPIException): """ Entity not found on database. """ status_code: int = field(default=404, init=False) message: str = field(default="The requested entity was not " "found in database", init=False)
[docs]@dataclass class NoRowsAffectedException(NovaAPIException): """ No rows affected on database. """ status_code: int = field(default=304, init=False) message: str = field(default="No rows were affected by the " "desired operation", init=False) error_code: int = field(default=304, init=False)
[docs]@dataclass class InvalidAttributeException(NovaAPIException): """ Attribute failed it's validation. """ status_code: int = field(default=400, init=False) message: str = field(default='Invalid attribute value', init=False)