errors.py 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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 LinkingTimeout(LinkingError):
  21. pass
  22. class LinkingConflict(LinkingError):
  23. pass
  24. def make_linking_error(data: Dict[str, Any]) -> LinkingError:
  25. message = data["message"]
  26. msg_number = data.get("msg_number")
  27. return {
  28. 1: LinkingTimeout,
  29. 3: LinkingConflict,
  30. }.get(msg_number, LinkingError)(message, msg_number)