errors.py 3.1 KB

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