error.py 650 B

123456789101112131415161718192021222324252627282930313233343536
  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}."