選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

exc.py 1.9KB

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