2017-08-25 16:34:20 +03:00
|
|
|
import urllib.request
|
2017-06-10 12:47:51 +03:00
|
|
|
import re
|
2017-09-25 12:56:44 +03:00
|
|
|
from threading import Thread
|
2017-06-10 12:47:51 +03:00
|
|
|
|
|
|
|
from .common import (
|
|
|
|
ReadCancelledError, InvalidParameterError, TypeNotFoundError,
|
2017-09-17 20:14:36 +03:00
|
|
|
InvalidChecksumError, BrokenAuthKeyError, SecurityError,
|
|
|
|
CdnFileTamperedError
|
2017-06-10 12:47:51 +03:00
|
|
|
)
|
|
|
|
|
2017-10-20 16:44:43 +03:00
|
|
|
# This imports the base errors too, as they're imported there
|
|
|
|
from .rpc_error_list import *
|
2017-06-10 12:47:51 +03:00
|
|
|
|
|
|
|
|
2017-09-25 12:56:44 +03:00
|
|
|
def report_error(code, message, report_method):
|
|
|
|
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={}'
|
2017-09-25 13:00:09 +03:00
|
|
|
.format(code, message, report_method),
|
|
|
|
timeout=5
|
2017-09-25 12:56:44 +03:00
|
|
|
)
|
|
|
|
url.read()
|
|
|
|
url.close()
|
|
|
|
except:
|
|
|
|
"We really don't want to crash when just reporting an error"
|
|
|
|
|
|
|
|
|
2017-08-25 16:34:20 +03:00
|
|
|
def rpc_message_to_error(code, message, report_method=None):
|
|
|
|
if report_method is not None:
|
2017-09-25 12:56:44 +03:00
|
|
|
Thread(
|
2017-09-25 12:57:42 +03:00
|
|
|
target=report_error,
|
2017-09-25 12:56:44 +03:00
|
|
|
args=(code, message, report_method)
|
|
|
|
).start()
|
2017-08-25 16:34:20 +03:00
|
|
|
|
2017-10-20 16:44:43 +03:00
|
|
|
# Try to get the error by direct look-up, otherwise regex
|
|
|
|
# TODO Maybe regexes could live in a separate dictionary?
|
|
|
|
cls = rpc_errors_all.get(message, None)
|
|
|
|
if cls:
|
|
|
|
return cls()
|
|
|
|
|
|
|
|
for msg_regex, cls in rpc_errors_all.items():
|
|
|
|
m = re.match(msg_regex, message)
|
|
|
|
if m:
|
|
|
|
capture = int(m.group(1)) if m.groups() else None
|
|
|
|
return cls(capture=capture)
|
|
|
|
|
|
|
|
if code == 400:
|
|
|
|
return BadRequestError(message)
|
2017-06-10 12:47:51 +03:00
|
|
|
|
2017-10-20 16:44:43 +03:00
|
|
|
if code == 401:
|
|
|
|
return UnauthorizedError(message)
|
2017-06-10 12:47:51 +03:00
|
|
|
|
2017-10-20 16:44:43 +03:00
|
|
|
if code == 403:
|
2017-06-17 09:25:48 +03:00
|
|
|
return ForbiddenError(message)
|
2017-06-10 12:47:51 +03:00
|
|
|
|
2017-10-20 16:44:43 +03:00
|
|
|
if code == 404:
|
2017-06-17 09:25:48 +03:00
|
|
|
return NotFoundError(message)
|
2017-06-10 12:47:51 +03:00
|
|
|
|
2017-10-20 16:44:43 +03:00
|
|
|
if 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))
|