matrix-appservice-kakaotalk/matrix_appservice_kakaotalk/kt/client/errors.py

161 lines
8.2 KiB
Python
Raw Normal View History

2022-02-25 02:22:50 -05:00
# matrix-appservice-kakaotalk - A Matrix-KakaoTalk puppeting bridge.
# Copyright (C) 2022 Tulir Asokan, Andrew Ferrazzutti
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
"""Helper functions & types for status codes for the KakaoTalk API."""
from __future__ import annotations
from typing import Type
from ..types.api.auth_api_client import KnownAuthStatusCode
from ..types.request import KnownDataStatusCode, RootCommandResult
class CommandException(Exception):
def __init__(self, result: RootCommandResult):
"""
Base type for errors raised from KakaoTalk. Subclasses identify different kinds of errors.
Which subclass to use should be based on the status code of the "result" object.
In the case that different status codes map to the same error subclass, the status code
can be retrieved from the "status" property.
"""
# NOTE unsuccessful responses do not set a result, hence using RootCommandResult here
# TODO Print _unrecognized?
self.status = result.status
self.message = _status_code_message_map.get(self.status, self._default_message)
@property
def _default_message(self):
return "Unknown error"
def __str__(self):
return f"{self.message} ({self.status})"
class OAuthException(CommandException):
pass
class DeviceVerificationRequired(OAuthException):
pass
class IncorrectPassword(OAuthException):
pass
class IncorrectPasscode(OAuthException):
pass
class InvalidAccessToken(OAuthException):
"""Thrown when the session token is invalid and must be refreshed"""
@property
def _default_message(self):
return "Invalid access token"
class AuthenticationRequired(OAuthException):
"""Thrown when both the session token and refresh token are invalid"""
pass
class ResponseError(Exception):
"""TODO Use for network failures to the KakaoTalk API"""
pass
_status_code_message_map: dict[KnownAuthStatusCode | KnownDataStatusCode | int] = {
KnownAuthStatusCode.INVALID_PHONE_NUMBER: "Invalid phone number",
KnownAuthStatusCode.SUCCESS_WITH_ACCOUNT: "Success",
KnownAuthStatusCode.SUCCESS_WITH_DEVICE_CHANGED: "Success (device changed)",
KnownAuthStatusCode.MISMATCH_PASSWORD: "Password mismatch",
KnownAuthStatusCode.EXCEED_LOGIN_LIMIT: "Login limit exceeded",
KnownAuthStatusCode.MISMATCH_PHONE_NUMBER: "Phone number mismatch",
KnownAuthStatusCode.EXCEED_PHONE_NUMBER_CHECK_LIMIT: "Phone number limit exceeded",
KnownAuthStatusCode.NOT_EXIST_ACCOUNT: "Account does not exist",
KnownAuthStatusCode.NEED_CHECK_PHONE_NUMBER: "Must check phone number",
KnownAuthStatusCode.NEED_CHECK_QUIZ: "Must check quiz",
KnownAuthStatusCode.DORMANT_ACCOUNT: "Dormant account",
KnownAuthStatusCode.RESTRICTED_ACCOUNT: "Restricted account",
KnownAuthStatusCode.LOGIN_FAILED: "Login failed",
KnownAuthStatusCode.NOT_VERIFIED_EMAIL: "Unverified email address",
KnownAuthStatusCode.MOBILE_UNREGISTERED: "Mobile device not registered",
KnownAuthStatusCode.UNKNOWN_PHONE_NUMBER: "Unknown phone number",
KnownAuthStatusCode.SUCCESS_SAME_USER: "Success (same user)",
KnownAuthStatusCode.SUCCESS_SAME_USER_BY_MIGRATION: "Success (same user by migration)",
KnownAuthStatusCode.TOO_MANY_REQUEST_A_DAY: "Too many requests a day",
KnownAuthStatusCode.TOO_MANY_REQUEST_AT_A_TIME: " Too many requests at a time",
KnownAuthStatusCode.MISMATCH_PASSCODE: "Passcode mismatch",
KnownAuthStatusCode.EXCEED_DAILY_REQUEST_LIMIT: "Daily request limit exceeded",
KnownAuthStatusCode.EXCEED_DAILY_REQUEST_LIMIT_VOICECALL: "Daily voicecall limit exceeded",
KnownAuthStatusCode.EXCEED_DAILY_REQUEST_LIMIT_WITHOUT_TOKEN: "Daily tokenless request limit exceeded",
KnownAuthStatusCode.DEVICE_NOT_REGISTERED: "Device not registered",
KnownAuthStatusCode.ANOTHER_LOGON: "Another logon detected",
KnownAuthStatusCode.DEVICE_REGISTER_FAILED: "Device registration failed",
KnownAuthStatusCode.INVALID_DEVICE_REGISTER: "Invalid device",
KnownAuthStatusCode.INVALID_PASSCODE: "Invalid passcode",
KnownAuthStatusCode.PASSCODE_REQUEST_FAILED: "Passcode request failed",
KnownAuthStatusCode.NEED_TERMS_AGREE: "Must agree to terms",
KnownAuthStatusCode.DENIED_DEVICE_MODEL: "Denied device model",
KnownAuthStatusCode.RESET_STEP: "Reset step",
KnownAuthStatusCode.NEED_PROTECTOR_AGREE: "Must agree to protector terms",
KnownAuthStatusCode.ACCOUNT_RESTRICTED: "Account restricted",
#KnownAuthStatusCode.INVALID_STAGE_ERROR: "Same as KnownAuthStatusCode.AUTH_REQUIRED",
KnownAuthStatusCode.UPGRADE_REQUIRED: "Upgrade required",
KnownAuthStatusCode.VOICE_CALL_ONLY: "Voice call only",
KnownAuthStatusCode.ACCESSIBILITY_ARS_ONLY: "Accessibility only",
KnownAuthStatusCode.MIGRATION_FAILURE: "Migration failure",
KnownAuthStatusCode.INVAILD_TOKEN: "Invalid token",
KnownAuthStatusCode.UNDEFINED: "Undefined error",
KnownDataStatusCode.SUCCESS: "Success",
KnownDataStatusCode.INVALID_USER: "Invalid user",
KnownDataStatusCode.CLIENT_ERROR: "Client error",
KnownDataStatusCode.NOT_LOGON: "Not logged in",
KnownDataStatusCode.INVALID_METHOD: "Invalid method",
KnownDataStatusCode.INVALID_PARAMETER: "Invalid parameter",
KnownDataStatusCode.INVALID_BODY: "Invalid body",
KnownDataStatusCode.INVALID_HEADER: "Invalid header",
KnownDataStatusCode.UNAUTHORIZED_CHAT_DELETE: "Unauthorized chat deletion",
KnownDataStatusCode.MEDIA_SERVER_ERROR: "Media server error",
KnownDataStatusCode.CHAT_SPAM_LIMIT: "Chat spam limit exceeded",
KnownDataStatusCode.RESTRICTED_APP: "Restricted app",
KnownDataStatusCode.LOGINLIST_CHATLIST_FAILED: "Login chat list failed",
KnownDataStatusCode.MEDIA_NOT_FOUND: "Media not found",
KnownDataStatusCode.MEDIA_THUMB_GEN_FAILED: "Could not generate media thumbnail",
KnownDataStatusCode.UNSUPPORTED: "Unsupported",
KnownDataStatusCode.PARTIAL: "Parial",
KnownDataStatusCode.LINK_JOIN_TPS_EXCEEDED: "Link join TPS exceeded",
KnownDataStatusCode.CHAT_SEND_RESTRICTED: "Chat send restricted",
KnownDataStatusCode.CHANNEL_CREATE_TEMP_RESTRICTED: "Channel creation temporarily restricted",
KnownDataStatusCode.CHANNEL_CREATE_RESTRICTED: "Channel creation restricted",
KnownDataStatusCode.OPENLINK_UNAVAILABLE: "Openlink unavailable",
KnownDataStatusCode.INVITE_COUNT_LIMITED: "Invite count limited",
KnownDataStatusCode.OPENLINK_CREATE_RESTRICTED: "Openlink creation restricted",
KnownDataStatusCode.INVALID_CHANNEL: "Invalid channel",
KnownDataStatusCode.CHAT_BLOCKED_BY_FRIEND: "Blocked by friend",
KnownDataStatusCode.NOT_CHATABLE_USER: "Non-chattable user",
KnownDataStatusCode.GAME_MESSAGE_BLOCKED: "Game message blocked",
KnownDataStatusCode.BLOCKED_IP: "Blocked IP",
KnownDataStatusCode.BACKGROUND_LOGIN_BLOCKED: "Background login blocked",
KnownDataStatusCode.OPERATION_DENIED: "Operation denied",
KnownDataStatusCode.CHANNEL_USER_LIMITED: "Channel user limited",
KnownDataStatusCode.TEMP_RESTRICTED: "Temporarily restricted",
KnownDataStatusCode.WRITE_WHILE_BLOCKED: "Write while blocked",
KnownDataStatusCode.OPENCHAT_REJOIN_REQUIRED: "Openchat rejoin required",
KnownDataStatusCode.OPENCHAT_TIME_RESTRICTED: "Openchat time restricted",
KnownDataStatusCode.INVALID_ACCESS_TOKEN: "Invalid access token",
#KnownDataStatusCode.BLOCKED_ACCOUNT: "Same as KnownAuthStatusCode.ACCOUNT_RESTRICTED"
KnownDataStatusCode.AUTH_REQUIRED: "Authentication required",
#KnownDataStatusCode.UPDATE_REQUIRED = "Same as KnownAuthStatusCode.UPGRADE_REQUIRED"
KnownDataStatusCode.SERVER_UNDER_MAINTENANCE: "Server under maintenance",
}