error.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 BadPayload(LCException):
  14. key: str
  15. def __str__(self):
  16. return f"Missing value for '{self.key}' in request"
  17. def http_code(self) -> int:
  18. return 400
  19. @dataclass
  20. class UserExists(LCException):
  21. name: str
  22. def __str__(self):
  23. return f"A user named '{self.name}' already exists."
  24. @dataclass
  25. class NoSuchUser(LCException):
  26. name: str
  27. def __str__(self):
  28. return f"No user named '{self.name}' exists."
  29. def http_code(self) -> int:
  30. return 404
  31. @dataclass
  32. class NoSuchLink(LCException):
  33. link_id: int
  34. def __str__(self):
  35. return f"No link '{self.link_id}' exists."
  36. def http_code(self) -> int:
  37. return 404
  38. @dataclass
  39. class BadPassword(LCException):
  40. name: str
  41. def __str__(self):
  42. return f"Wrong password for user {self.name}."
  43. def http_code(self) -> int:
  44. return 403
  45. @dataclass
  46. class NotImplemented(LCException):
  47. def __str__(self):
  48. return "Bad request: no handler for route."
  49. def http_code(self) -> int:
  50. return 404
  51. @dataclass
  52. class BadPermissions(LCException):
  53. def __str__(self):
  54. return "Insufficient permissions."
  55. def http_code(self) -> int:
  56. return 403
  57. @dataclass
  58. class BadContentType(LCException):
  59. content_type: str
  60. def __str__(self):
  61. return f"Bad content type for request: {self.content_type}"
  62. def http_code(self) -> int:
  63. return 403
  64. @dataclass
  65. class NoSuchInvite(LCException):
  66. invite: str
  67. def __str__(self):
  68. return f"No such invite code: {self.invite}."
  69. def http_code(self) -> int:
  70. return 404
  71. @dataclass
  72. class AlreadyUsedInvite(LCException):
  73. invite: str
  74. def __str__(self):
  75. return f"Invite code {self.invite} already taken."
  76. def http_code(self) -> int:
  77. return 403
  78. @dataclass
  79. class MismatchedPassword(LCException):
  80. def __str__(self):
  81. return "Provided passwords do not match. Please check your passwords."
  82. def http_code(self) -> int:
  83. return 400
  84. @dataclass
  85. class BadTagName(LCException):
  86. tag_name: str
  87. def __str__(self):
  88. return f"'{self.tag_name}' is not a valid tag name, for Reasons."
  89. def http_code(self) -> int:
  90. return 400
  91. @dataclass
  92. class BadAddLink(LCException):
  93. message: str
  94. def __str__(self):
  95. return f"Error adding link: {self.message}"
  96. def http_code(self) -> int:
  97. return 400
  98. @dataclass
  99. class BadFileUpload(LCException):
  100. message: str
  101. def __str__(self):
  102. return f"Problem with uploaded file: {self.message}"
  103. def http_code(self) -> int:
  104. return 400