This repository has been archived on 2024-01-13. You can view files and clone it, but cannot push or open issues or pull requests.
arka-mvp/api/api_errors.py

48 lines
1.4 KiB
Python

import traceback
# как создавать ошибку
# raise Exception(API_ERROR_XXX, <related_obj>)
API_ERROR_INTERNAL_ERROR = (100, 'internal error')
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_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')
def make_error_object(ex: Exception):
try:
if type(ex.args[0]) != tuple:
raise Exception(API_ERROR_INTERNAL_ERROR)
data = {
"error": {
"code": ex.args[0][0],
"message": ex.args[0][1]
}
}
if len(ex.args) >= 2:
data["error"]["related"] = ex.args[1]
return data
except Exception:
traceback.print_exc()
return {
"error": {
"code": API_ERROR_INTERNAL_ERROR[0],
"message": API_ERROR_INTERNAL_ERROR[1]
}
}