Error reporting module¶
Quick Start¶
In NovaAPI it’s possible to use custom exceptions to generate error responses with custom status codes, messages, and data objects. It’s actually quite simple to do it and you may subclass the NovaAPIException class or use it as is. Below are two examples, one with each approach:
@use_dao(MyEntityDAO, "Default error message")
def read(id_: str):
if len(id_) < 5: # Some arbitrary requirement
raise NovaAPIException(status_code=400, message="Id is not valid!",
error_code=3, debug=f"{id_} is less than 5 chars long.")
Now, an example with the custom class could look like:
@dataclass
class InvalidIDException(NovaAPIException):
status_code: int = field(default=400, init=False)
message: str = field(default="ID is invalid!", init=False)
error_code: int = field(default=1, init=False)
debug: str = "ID not valid"
...
@use_dao(MyEntityDAO, "Default error message")
def read(id_: str):
if len(id_) < 5:
raise InvalidIDException(f"{id_} received")
Module Documentation¶
Custom exceptions for NovaAPI
-
exception
nova_api.exceptions.DuplicateEntityException(error_code: Optional[int] = None, debug: str = 'No debug information')[source]¶ Entity UUID already exists on database.
-
exception
nova_api.exceptions.EntityNotFoundException(error_code: Optional[int] = None, debug: str = 'No debug information')[source]¶ Entity not found on database.
-
exception
nova_api.exceptions.InvalidAttributeException(error_code: Optional[int] = None, debug: str = 'No debug information')[source]¶ Attribute failed it’s validation.
-
exception
nova_api.exceptions.InvalidFiltersException(error_code: Optional[int] = None, debug: str = 'No debug information')[source]¶ Filters is not a dictionary
-
exception
nova_api.exceptions.InvalidIDException(error_code: Optional[int] = None, debug: str = 'No debug information')[source]¶ ID is not a valid UUID v4
-
exception
nova_api.exceptions.InvalidIDTypeException(error_code: Optional[int] = None, debug: str = 'No debug information')[source]¶ ID is not a string
-
exception
nova_api.exceptions.NoRowsAffectedException(debug: str = 'No debug information')[source]¶ No rows affected on database.
-
exception
nova_api.exceptions.NotEntityException(error_code: Optional[int] = None, debug: str = 'No debug information')[source]¶ Argument is not an Entity
-
exception
nova_api.exceptions.NovaAPIException(status_code: int = 500, message: str = 'Error', error_code: Optional[int] = None, debug: str = 'No debug information')[source]¶ 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.
Parameters: - status_code – The integer HTTP status code to send in the response. Defaults to 500.
- message – The message to send in the message field of the response. Defaults to Error.
- 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.
- debug – The debug message to send in the data object if DEBUG is True. Defaults to “No debug information”.