error.py 992 B

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