little-boxes/little_boxes/errors.py

93 wiersze
2.4 KiB
Python
Czysty Zwykły widok Historia

2018-06-11 20:50:02 +00:00
"""Errors raised by this package."""
from typing import Any
2018-06-15 22:27:49 +00:00
from typing import Dict
from typing import Optional
2018-06-11 20:50:02 +00:00
class Error(Exception):
2018-06-22 21:38:25 +00:00
"""Base error for exceptions raised by this package."""
2018-07-01 08:38:35 +00:00
class DropActivityPreProcessError(Error):
"""Raised in `_pre_process_from_inbox` to notify that we don't want to save the message.
(like when receiving `Announce` with an OStatus link).
"""
2018-06-22 21:38:25 +00:00
class ServerError(Error):
2018-06-11 20:50:02 +00:00
"""HTTP-friendly base error, with a status code, a message and an optional payload."""
2018-06-12 17:57:40 +00:00
2018-06-11 20:50:02 +00:00
status_code = 400
def __init__(
2018-06-12 17:57:40 +00:00
self,
message: str,
2018-06-11 20:50:02 +00:00
status_code: Optional[int] = None,
payload: Optional[Dict[str, Any]] = None,
) -> None:
Exception.__init__(self)
self.message = message
if status_code is not None:
self.status_code = status_code
self.payload = payload
def to_dict(self) -> Dict[str, Any]:
2018-06-13 18:08:12 +00:00
rv = dict(self.payload or {})
2018-06-12 17:57:40 +00:00
rv["message"] = self.message
2018-06-11 20:50:02 +00:00
return rv
def __repr__(self) -> str: # pragma: no cover
2018-06-13 07:38:12 +00:00
return (
f"{self.__class__.__qualname__}({self.message!r}, "
f"payload={self.payload!r}, status_code={self.status_code})"
)
2018-06-11 20:50:02 +00:00
def __str__(self) -> str: # pragma: no cover
return self.__repr__()
2018-06-11 20:50:02 +00:00
2018-06-22 21:38:25 +00:00
class ActorBlockedError(ServerError):
2018-06-11 20:50:02 +00:00
"""Raised when an activity from a blocked actor is received."""
2018-06-22 21:38:25 +00:00
class NotFromOutboxError(ServerError):
2018-06-11 20:50:02 +00:00
"""Raised when an activity targets an object from the inbox when an object from the oubox was expected."""
2018-06-22 21:38:25 +00:00
class ActivityNotFoundError(ServerError):
2018-06-11 20:50:02 +00:00
"""Raised when an activity is not found."""
2018-06-12 17:57:40 +00:00
2018-06-11 20:50:02 +00:00
status_code = 404
2018-07-09 22:23:35 +00:00
class ActivityGoneError(ServerError):
"""Raised when trying to fetch a remote activity that was deleted."""
status_code = 410
2018-06-22 21:38:25 +00:00
class BadActivityError(ServerError):
2018-06-11 20:50:02 +00:00
"""Raised when an activity could not be parsed/initialized."""
class RecursionLimitExceededError(BadActivityError):
"""Raised when the recursion limit for fetching remote object was exceeded (likely a collection)."""
class UnexpectedActivityTypeError(BadActivityError):
"""Raised when an another activty was expected."""
2018-07-08 21:02:15 +00:00
class ActivityUnavailableError(ServerError):
"""Raises when fetching a remote activity times out."""
status_code = 503
2018-07-22 09:18:27 +00:00
class NotAnActivityError(ServerError):
"""Raised when no JSON can be decoded.
Most likely raised when stumbling upon a OStatus notice or failed lookup.
"""