response.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. # mautrix-instagram - A Matrix-Instagram puppeting bridge.
  2. # Copyright (C) 2020 Tulir Asokan
  3. #
  4. # This program is free software: you can redistribute it and/or modify
  5. # it under the terms of the GNU Affero General Public License as published by
  6. # the Free Software Foundation, either version 3 of the License, or
  7. # (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU Affero General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU Affero General Public License
  15. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. from typing import Optional, get_type_hints
  17. from aiohttp import ClientResponse
  18. from mautrix.types import JSON, Serializable
  19. from ..types import (
  20. ChallengeResponse,
  21. CheckpointResponse,
  22. ConsentRequiredResponse,
  23. LoginErrorResponse,
  24. LoginRequiredResponse,
  25. SpamResponse,
  26. )
  27. from .base import IGError
  28. class IGChallengeWrongCodeError(IGError):
  29. pass
  30. class IGUnknownError(IGError):
  31. response: ClientResponse
  32. def __init__(self, response: ClientResponse) -> None:
  33. super().__init__(
  34. f"Request {response.request_info.method} {response.request_info.url.path} failed: "
  35. f"HTTP {response.status} with non-JSON body"
  36. )
  37. self.response = response
  38. class IGResponseError(IGError):
  39. response: ClientResponse
  40. def __init__(self, response: ClientResponse, json: JSON) -> None:
  41. prefix = f"Request {response.request_info.method} {response.request_info.url.path} failed"
  42. message = f"HTTP {response.status}"
  43. self.response = response
  44. if "message" in json:
  45. message = json["message"]
  46. type_hint = get_type_hints(type(self)).get("body", JSON)
  47. if type_hint is not JSON and issubclass(type_hint, Serializable):
  48. self.body = type_hint.deserialize(json)
  49. else:
  50. self.body = json
  51. super().__init__(f"{prefix}: {self._message_override or message}")
  52. @property
  53. def _message_override(self) -> Optional[str]:
  54. return None
  55. class IGActionSpamError(IGResponseError):
  56. body: SpamResponse
  57. @property
  58. def _message(self) -> str:
  59. return f"HTTP {self.body.message}"
  60. class IGNotFoundError(IGResponseError):
  61. pass
  62. class IGRateLimitError(IGResponseError):
  63. pass
  64. class IGCheckpointError(IGResponseError):
  65. body: CheckpointResponse
  66. class IGChallengeError(IGResponseError):
  67. body: ChallengeResponse
  68. @property
  69. def url(self) -> str:
  70. return self.body.challenge.api_path
  71. class IGConsentRequiredError(IGResponseError):
  72. body: ConsentRequiredResponse
  73. class IGNotLoggedInError(IGResponseError):
  74. body: LoginRequiredResponse
  75. @property
  76. def proper_message(self) -> str:
  77. return (
  78. f"{self.body.error_title or self.body.message} "
  79. f"(reason code: {self.body.logout_reason})"
  80. )
  81. class IGUserHasLoggedOutError(IGNotLoggedInError):
  82. pass
  83. class IGLoginRequiredError(IGNotLoggedInError):
  84. pass
  85. class IGPrivateUserError(IGResponseError):
  86. pass
  87. class IGSentryBlockError(IGResponseError):
  88. pass
  89. class IGInactiveUserError(IGResponseError):
  90. pass
  91. class IGLoginError(IGResponseError):
  92. body: LoginErrorResponse
  93. class IGLoginTwoFactorRequiredError(IGLoginError):
  94. pass
  95. class IGLoginBadPasswordError(IGLoginError):
  96. pass
  97. class IGLoginUnusablePasswordError(IGLoginError):
  98. pass
  99. class IGLoginInvalidUserError(IGLoginError):
  100. pass
  101. class IGFBNoContactPointFoundError(IGLoginError):
  102. pass
  103. class IGBad2FACodeError(IGResponseError):
  104. pass