refactoring

This commit is contained in:
Devaev Maxim
2020-05-29 07:15:04 +03:00
parent 0ead2f45cf
commit ef8b6cfda5
2 changed files with 13 additions and 11 deletions

View File

@@ -24,17 +24,19 @@ from ...validators import ValidatorError
# =====
class HttpError(Exception):
pass
def __init__(self, msg: str, status: int) -> None:
super().__init__(msg)
self.status = status
class UnauthorizedError(HttpError):
def __init__(self) -> None:
super().__init__("Unauthorized")
super().__init__("Unauthorized", 401)
class ForbiddenError(HttpError):
def __init__(self) -> None:
super().__init__("Forbidden")
super().__init__("Forbidden", 403)
# =====
@@ -126,11 +128,14 @@ def make_json_response(
return response
def make_json_exception(err: Exception, status: int) -> aiohttp.web.Response:
def make_json_exception(err: Exception, status: Optional[int]=None) -> aiohttp.web.Response:
name = type(err).__name__
msg = str(err)
if not isinstance(err, (UnauthorizedError, ForbiddenError)):
if isinstance(err, HttpError):
status = err.status
else:
get_logger().error("API error: %s: %s", name, msg)
assert status is not None, err
return make_json_response({
"error": name,
"error_msg": msg,