You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

exc.py 1.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. from collections.abc import Mapping
  2. from pydantic import TypeAdapter
  3. class PluginDaemonError(Exception):
  4. """Base class for all plugin daemon errors."""
  5. def __init__(self, description: str) -> None:
  6. self.description = description
  7. def __str__(self) -> str:
  8. # returns the class name and description
  9. return f"{self.__class__.__name__}: {self.description}"
  10. class PluginDaemonInternalError(PluginDaemonError):
  11. pass
  12. class PluginDaemonClientSideError(PluginDaemonError):
  13. pass
  14. class PluginDaemonInternalServerError(PluginDaemonInternalError):
  15. description: str = "Internal Server Error"
  16. class PluginDaemonUnauthorizedError(PluginDaemonInternalError):
  17. description: str = "Unauthorized"
  18. class PluginDaemonNotFoundError(PluginDaemonInternalError):
  19. description: str = "Not Found"
  20. class PluginDaemonBadRequestError(PluginDaemonClientSideError):
  21. description: str = "Bad Request"
  22. class PluginInvokeError(PluginDaemonClientSideError):
  23. description: str = "Invoke Error"
  24. def _get_error_object(self) -> Mapping:
  25. try:
  26. return TypeAdapter(Mapping).validate_json(self.description)
  27. except Exception:
  28. return {}
  29. def get_error_type(self) -> str:
  30. return self._get_error_object().get("error_type", "unknown")
  31. def get_error_message(self) -> str:
  32. try:
  33. return self._get_error_object().get("message", "unknown")
  34. except Exception:
  35. return self.description
  36. class PluginUniqueIdentifierError(PluginDaemonClientSideError):
  37. description: str = "Unique Identifier Error"
  38. class PluginNotFoundError(PluginDaemonClientSideError):
  39. description: str = "Plugin Not Found"
  40. class PluginPermissionDeniedError(PluginDaemonClientSideError):
  41. description: str = "Permission Denied"