100 lines
3.7 KiB
Python
Executable File
100 lines
3.7 KiB
Python
Executable File
import traceback
|
|
from .api_phone_verificator import PhoneVerificationService
|
|
from arka.settings import PHONE_VERIFICATION_RESEND_TIME_SECS
|
|
|
|
# как создавать ошибку
|
|
# raise Exception(API_ERROR_XXX, <related_obj>)
|
|
|
|
API_OK_OBJ = {"status": "success"}
|
|
|
|
API_ERROR_INTERNAL_ERROR = (100, 'internal error')
|
|
|
|
API_ERROR_MULTIPLY_ERRORS = (101, 'multiply errors')
|
|
API_ERROR_NOT_FOUND = (102, 'object not found')
|
|
API_ERROR_ACCESS_DENIED = (103, 'you cannot call this method: permission denied')
|
|
API_ERROR_NEED_COMPLETED_ACCOUNT = (104, 'need completed account')
|
|
API_ERROR_NOT_ALLOWED = (105, 'operation not allowed')
|
|
|
|
API_ERROR_INVALID_REQUEST = (110, 'invalid request')
|
|
|
|
API_ERROR_METHOD_NOT_FOUND = (200, 'method not found')
|
|
API_ERROR_MISSING_ARGUMENT = (201, 'missing argument')
|
|
API_ERROR_UNKNOWN_ARGUMENT = (202, 'unknown argument')
|
|
API_ERROR_INVALID_ARGUMENT_TYPE = (203, 'invalid argument type')
|
|
API_ERROR_INVALID_ARGUMENT_VALUE = (204, 'invalid argument value')
|
|
|
|
API_ERROR_TOKEN_CREATION = (500, 'token creation error')
|
|
|
|
API_ERROR_INVALID_LOGIN = (501, 'invalid login')
|
|
API_ERROR_INVALID_PASSWORD = (502, 'invalid password')
|
|
API_ERROR_INVALID_TOKEN = (503, 'invalid token')
|
|
|
|
# времненное решение, позже нужно будет заменить на конкретные ошибки
|
|
API_ERROR_USER_REGISTER = (510, 'user registration error')
|
|
API_ERROR_NEED_VERIFY = (511, 'need verification code')
|
|
API_ERROR_USER_MODIFY = (512, 'user modification error')
|
|
API_ERROR_OBJECT_VALIDATION = (513, 'object validation error')
|
|
|
|
|
|
API_ERROR_VERIFY_INVALID_CODE = (520, 'invalid code')
|
|
API_ERROR_VERIFY_MAX_ATTEMPTS = (521, 'max attempts')
|
|
API_ERROR_CURRENTLY_VERIFIED = (522, 'currently phone is verified')
|
|
API_ERROR_VERIFY_FAILED = (523, 'cannot be verified')
|
|
API_ERROR_VERIFY_NOT_READY = (524, 'verification service not ready. call this method later')
|
|
API_ERROR_VERIFY_NOT_FOUND = (525, 'verification service did not send code. call this method without \'code\'')
|
|
API_ERROR_VERIFY_RESEND_LIMIT = (526, f'resend verification limit '
|
|
f'(one verify for {PHONE_VERIFICATION_RESEND_TIME_SECS} secs)')
|
|
API_ERROR_VERIFY_UNKNOWN = (527, 'unknown verification error')
|
|
|
|
API_ERROR_VERIFICATION = {
|
|
PhoneVerificationService.CHECK_PHONE_INVALID_CODE: API_ERROR_VERIFY_INVALID_CODE,
|
|
PhoneVerificationService.CHECK_PHONE_MAX_ATTEMPTS: API_ERROR_VERIFY_MAX_ATTEMPTS,
|
|
PhoneVerificationService.CHECK_PHONE_FAILED: API_ERROR_VERIFY_FAILED,
|
|
PhoneVerificationService.CHECK_PHONE_NOT_READY: API_ERROR_VERIFY_NOT_READY,
|
|
PhoneVerificationService.CHECK_PHONE_NOT_FOUND: API_ERROR_VERIFY_NOT_FOUND,
|
|
PhoneVerificationService.CHECK_PHONE_RESEND_LIMIT: API_ERROR_VERIFY_RESEND_LIMIT,
|
|
}
|
|
|
|
|
|
def __make_error(ex: Exception):
|
|
if type(ex.args[0]) != tuple:
|
|
raise ex
|
|
|
|
error = {
|
|
"code": ex.args[0][0],
|
|
"message": ex.args[0][1]
|
|
}
|
|
|
|
if len(ex.args) >= 2:
|
|
error["related"] = ex.args[1]
|
|
|
|
return error
|
|
|
|
|
|
def make_error_object(ex: Exception | list):
|
|
try:
|
|
data = {
|
|
"status": "error"
|
|
}
|
|
if type(ex) == list:
|
|
data["error"] = {
|
|
"code": API_ERROR_MULTIPLY_ERRORS[0],
|
|
"message": API_ERROR_MULTIPLY_ERRORS[1],
|
|
}
|
|
data["related"] = [__make_error(e) for e in ex]
|
|
else:
|
|
data["error"] = __make_error(ex)
|
|
|
|
return data
|
|
except BaseException as err:
|
|
traceback.print_exc()
|
|
|
|
return {
|
|
"status": "error",
|
|
"error": {
|
|
"code": API_ERROR_INTERNAL_ERROR[0],
|
|
"message": API_ERROR_INTERNAL_ERROR[1],
|
|
"related": f"Exception {type(err)}: {str(err)}"
|
|
}
|
|
}
|