error.py 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. from dataclasses import dataclass
  2. @dataclass
  3. class LCRedirect(Exception):
  4. path: str
  5. def to_path(self) -> str:
  6. return self.path
  7. class LCException(Exception):
  8. def to_json(self) -> dict:
  9. return {"error": str(self)}
  10. def http_code(self) -> int:
  11. return 500
  12. @dataclass
  13. class UserExists(LCException):
  14. name: str
  15. def __str__(self):
  16. return f"A user named {self.name} already exists."
  17. @dataclass
  18. class NoSuchUser(LCException):
  19. name: str
  20. def __str__(self):
  21. return f"No user named {self.name} exists."
  22. def http_code(self) -> int:
  23. return 404
  24. @dataclass
  25. class BadPassword(LCException):
  26. name: str
  27. def __str__(self):
  28. return f"Wrong password for user {self.name}."
  29. @dataclass
  30. class NotImplemented(LCException):
  31. def __str__(self):
  32. return f"Bad request: no handler for route."
  33. def http_code(self) -> int:
  34. return 404
  35. @dataclass
  36. class BadPermissions(LCException):
  37. def __str__(self):
  38. return f"Insufficient permissions."
  39. def http_code(self) -> int:
  40. return 400
  41. @dataclass
  42. class BadContentType(LCException):
  43. content_type: str
  44. def __str__(self):
  45. return f"Bad content type for request: {self.content_type}"
  46. def http_code(self) -> int:
  47. return 500