errors.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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, Optional
  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 NotConnected(RPCError):
  17. pass
  18. class ResponseError(RPCError):
  19. def __init__(
  20. self,
  21. data: Dict[str, Any],
  22. error_type: Optional[str] = None,
  23. message_override: Optional[str] = None,
  24. ) -> None:
  25. self.data = data
  26. msg = message_override or data["message"]
  27. if error_type:
  28. msg = f"{error_type}: {msg}"
  29. super().__init__(msg)
  30. class TimeoutException(ResponseError):
  31. pass
  32. class UnknownIdentityKey(ResponseError):
  33. pass
  34. class CaptchaRequired(ResponseError):
  35. pass
  36. class AuthorizationFailedException(ResponseError):
  37. pass
  38. class UserAlreadyExistsError(ResponseError):
  39. def __init__(self, data: Dict[str, Any]) -> None:
  40. super().__init__(data, message_override="You're already logged in")
  41. class RequestValidationFailure(ResponseError):
  42. def __init__(self, data: Dict[str, Any]) -> None:
  43. results = data["validationResults"]
  44. result_str = ", ".join(results) if isinstance(results, list) else str(results)
  45. super().__init__(data, message_override=result_str)
  46. class InternalError(ResponseError):
  47. def __init__(self, data: Dict[str, Any]) -> None:
  48. exceptions = data.get("exceptions", [])
  49. self.exceptions = exceptions
  50. message = data.get("message")
  51. super().__init__(data, error_type=", ".join(exceptions), message_override=message)
  52. response_error_types = {
  53. "invalid_request": RequestValidationFailure,
  54. "TimeoutException": TimeoutException,
  55. "UserAlreadyExists": UserAlreadyExistsError,
  56. "RequestValidationFailure": RequestValidationFailure,
  57. "UnknownIdentityKey": UnknownIdentityKey,
  58. "CaptchaRequired": CaptchaRequired,
  59. "AuthorizationFailedException": AuthorizationFailedException,
  60. "InternalError": InternalError,
  61. # TODO add rest from https://gitlab.com/signald/signald/-/tree/main/src/main/java/io/finn/signald/exceptions
  62. }
  63. def make_response_error(data: Dict[str, Any]) -> ResponseError:
  64. error_data = data["error"]
  65. if isinstance(error_data, str):
  66. error_data = {"message": error_data}
  67. elif not isinstance(error_data, dict):
  68. error_data = {"message": str(error_data)}
  69. if "message" not in error_data:
  70. error_data["message"] = "no message, see signald logs"
  71. error_type = data.get("error_type")
  72. try:
  73. error_class = response_error_types[error_type]
  74. except KeyError:
  75. return ResponseError(error_data, error_type=error_type)
  76. else:
  77. return error_class(error_data)