errors.py 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. # Copyright (c) 2020 Tulir Asokan
  2. #
  3. # This Source Code Form is subject to the terms of the Mozilla Public
  4. # License, v. 2.0. If a copy of the MPL was not distributed with this
  5. # file, You can obtain one at http://mozilla.org/MPL/2.0/.
  6. from typing import Any, Dict
  7. class RPCError(Exception):
  8. pass
  9. class UnexpectedError(RPCError):
  10. pass
  11. class UnexpectedResponse(RPCError):
  12. def __init__(self, resp_type: str, data: Any) -> None:
  13. super().__init__(f"Got unexpected response type {resp_type}")
  14. self.resp_type = resp_type
  15. self.data = data
  16. class LinkingError(RPCError):
  17. def __init__(self, message: str, number: int) -> None:
  18. super().__init__(message)
  19. self.number = number
  20. class NotConnected(RPCError):
  21. pass
  22. class LinkingTimeout(LinkingError):
  23. pass
  24. class LinkingConflict(LinkingError):
  25. pass
  26. def make_linking_error(data: Dict[str, Any]) -> LinkingError:
  27. message = data["message"]
  28. msg_number = data.get("msg_number")
  29. return {
  30. 1: LinkingTimeout,
  31. 3: LinkingConflict,
  32. }.get(msg_number, LinkingError)(message, msg_number)