response.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. ConsentRequiredResponse,
  22. LoginErrorResponse,
  23. LoginRequiredResponse,
  24. SpamResponse,
  25. )
  26. from .base import IGError
  27. class IGChallengeWrongCodeError(IGError):
  28. pass
  29. class IGUnknownError(IGError):
  30. response: ClientResponse
  31. def __init__(self, response: ClientResponse) -> None:
  32. super().__init__(
  33. f"Request {response.request_info.method} {response.request_info.url.path} failed: "
  34. f"HTTP {response.status} with non-JSON body"
  35. )
  36. self.response = response
  37. class IGResponseError(IGError):
  38. response: ClientResponse
  39. def __init__(self, response: ClientResponse, json: JSON) -> None:
  40. prefix = f"Request {response.request_info.method} {response.request_info.url.path} failed"
  41. message = f"HTTP {response.status}"
  42. self.response = response
  43. if "message" in json:
  44. message = json["message"]
  45. type_hint = get_type_hints(type(self)).get("body", JSON)
  46. if type_hint is not JSON and issubclass(type_hint, Serializable):
  47. self.body = type_hint.deserialize(json)
  48. else:
  49. self.body = json
  50. super().__init__(f"{prefix}: {self._message_override or message}")
  51. @property
  52. def _message_override(self) -> Optional[str]:
  53. return None
  54. class IGActionSpamError(IGResponseError):
  55. body: SpamResponse
  56. @property
  57. def _message(self) -> str:
  58. return f"HTTP {self.body.message}"
  59. class IGNotFoundError(IGResponseError):
  60. pass
  61. class IGRateLimitError(IGResponseError):
  62. pass
  63. class IGChallengeError(IGResponseError):
  64. body: ChallengeResponse
  65. @property
  66. def url(self) -> str:
  67. return self.body.challenge.api_path
  68. class IGConsentRequiredError(IGResponseError):
  69. body: ConsentRequiredResponse
  70. class IGNotLoggedInError(IGResponseError):
  71. body: LoginRequiredResponse
  72. @property
  73. def proper_message(self) -> str:
  74. return (
  75. f"{self.body.error_title or self.body.message} "
  76. f"(reason code: {self.body.logout_reason})"
  77. )
  78. class IGUserHasLoggedOutError(IGNotLoggedInError):
  79. pass
  80. class IGLoginRequiredError(IGNotLoggedInError):
  81. pass
  82. class IGPrivateUserError(IGResponseError):
  83. pass
  84. class IGSentryBlockError(IGResponseError):
  85. pass
  86. class IGInactiveUserError(IGResponseError):
  87. pass
  88. class IGLoginError(IGResponseError):
  89. body: LoginErrorResponse
  90. class IGLoginTwoFactorRequiredError(IGLoginError):
  91. pass
  92. class IGLoginBadPasswordError(IGLoginError):
  93. pass
  94. class IGLoginInvalidUserError(IGLoginError):
  95. pass
  96. class IGFBNoContactPointFoundError(IGLoginError):
  97. pass
  98. class IGBad2FACodeError(IGResponseError):
  99. pass