error.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. def http_code(self) -> int:
  30. return 403
  31. @dataclass
  32. class NotImplemented(LCException):
  33. def __str__(self):
  34. return f"Bad request: no handler for route."
  35. def http_code(self) -> int:
  36. return 404
  37. @dataclass
  38. class BadPermissions(LCException):
  39. def __str__(self):
  40. return f"Insufficient permissions."
  41. def http_code(self) -> int:
  42. return 403
  43. @dataclass
  44. class BadContentType(LCException):
  45. content_type: str
  46. def __str__(self):
  47. return f"Bad content type for request: {self.content_type}"
  48. def http_code(self) -> int:
  49. return 403
  50. @dataclass
  51. class NoSuchInvite(LCException):
  52. invite: str
  53. def __str__(self):
  54. return f"No such invite code: {self.invite}."
  55. def http_code(self) -> int:
  56. return 404
  57. @dataclass
  58. class AlreadyUsedInvite(LCException):
  59. invite: str
  60. def __str__(self):
  61. return f"Invite code {self.invite} already taken."
  62. def http_code(self) -> int:
  63. return 403
  64. @dataclass
  65. class MismatchedPassword(LCException):
  66. def __str__(self):
  67. return f"Provided passwords do not match. Please check your passwords."
  68. def http_code(self) -> int:
  69. return 400