2017-08-25 16:34:20 +03:00
|
|
|
import urllib.request
|
2017-06-10 12:47:51 +03:00
|
|
|
import re
|
|
|
|
|
|
|
|
from .common import (
|
|
|
|
ReadCancelledError, InvalidParameterError, TypeNotFoundError,
|
2017-08-28 15:17:31 +03:00
|
|
|
InvalidChecksumError, CdnFileTamperedError
|
2017-06-10 12:47:51 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
from .rpc_errors import (
|
|
|
|
RPCError, InvalidDCError, BadRequestError, UnauthorizedError,
|
|
|
|
ForbiddenError, NotFoundError, FloodError, ServerError, BadMessageError
|
|
|
|
)
|
|
|
|
|
|
|
|
from .rpc_errors_303 import *
|
|
|
|
from .rpc_errors_400 import *
|
|
|
|
from .rpc_errors_401 import *
|
|
|
|
from .rpc_errors_420 import *
|
|
|
|
|
|
|
|
|
2017-08-25 16:34:20 +03:00
|
|
|
def rpc_message_to_error(code, message, report_method=None):
|
|
|
|
if report_method is not None:
|
|
|
|
try:
|
|
|
|
# Ensure it's signed
|
|
|
|
report_method = int.from_bytes(
|
|
|
|
report_method.to_bytes(4, 'big'), 'big', signed=True
|
|
|
|
)
|
|
|
|
url = urllib.request.urlopen(
|
|
|
|
'https://rpc.pwrtelegram.xyz?code={}&error={}&method={}'
|
|
|
|
.format(code, message, report_method)
|
|
|
|
)
|
|
|
|
url.read()
|
|
|
|
url.close()
|
|
|
|
except:
|
|
|
|
"We really don't want to crash when just reporting an error"
|
|
|
|
|
2017-06-10 12:47:51 +03:00
|
|
|
errors = {
|
2017-06-24 19:10:05 +03:00
|
|
|
303: rpc_errors_303_all,
|
|
|
|
400: rpc_errors_400_all,
|
|
|
|
401: rpc_errors_401_all,
|
|
|
|
420: rpc_errors_420_all
|
2017-06-10 12:47:51 +03:00
|
|
|
}.get(code, None)
|
|
|
|
|
|
|
|
if errors is not None:
|
|
|
|
for msg, cls in errors.items():
|
|
|
|
m = re.match(msg, message)
|
|
|
|
if m:
|
|
|
|
extra = int(m.group(1)) if m.groups() else None
|
|
|
|
return cls(extra=extra)
|
|
|
|
|
|
|
|
elif code == 403:
|
2017-06-17 09:25:48 +03:00
|
|
|
return ForbiddenError(message)
|
2017-06-10 12:47:51 +03:00
|
|
|
|
|
|
|
elif code == 404:
|
2017-06-17 09:25:48 +03:00
|
|
|
return NotFoundError(message)
|
2017-06-10 12:47:51 +03:00
|
|
|
|
|
|
|
elif code == 500:
|
2017-06-17 09:25:48 +03:00
|
|
|
return ServerError(message)
|
2017-06-10 12:47:51 +03:00
|
|
|
|
|
|
|
return RPCError('{} (code {})'.format(message, code))
|