diff options
author | Devaev Maxim <[email protected]> | 2020-05-29 07:15:04 +0300 |
---|---|---|
committer | Devaev Maxim <[email protected]> | 2020-05-29 07:15:04 +0300 |
commit | ef8b6cfda56aa2474fde00c8e94ea0a3dafa3e0d (patch) | |
tree | e10d028c349f6f57f72a4041de30385ed8518310 /kvmd/apps | |
parent | 0ead2f45cf5edf39408909cef0c4591821781ad9 (diff) |
refactoring
Diffstat (limited to 'kvmd/apps')
-rw-r--r-- | kvmd/apps/kvmd/http.py | 15 | ||||
-rw-r--r-- | kvmd/apps/kvmd/server.py | 9 |
2 files changed, 13 insertions, 11 deletions
diff --git a/kvmd/apps/kvmd/http.py b/kvmd/apps/kvmd/http.py index 6d4c7c15..00b08014 100644 --- a/kvmd/apps/kvmd/http.py +++ b/kvmd/apps/kvmd/http.py @@ -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, diff --git a/kvmd/apps/kvmd/server.py b/kvmd/apps/kvmd/server.py index 9392c826..f6845a06 100644 --- a/kvmd/apps/kvmd/server.py +++ b/kvmd/apps/kvmd/server.py @@ -65,8 +65,7 @@ from .logreader import LogReader from .streamer import Streamer from .wol import WakeOnLan -from .http import UnauthorizedError -from .http import ForbiddenError +from .http import HttpError from .http import HttpExposed from .http import exposed_http from .http import exposed_ws @@ -280,10 +279,8 @@ class KvmdServer(HttpServer): # pylint: disable=too-many-arguments,too-many-ins return make_json_exception(err, 409) except (ValidatorError, OperationError) as err: return make_json_exception(err, 400) - except UnauthorizedError as err: - return make_json_exception(err, 401) - except ForbiddenError as err: - return make_json_exception(err, 403) + except HttpError as err: + return make_json_exception(err) app.router.add_route(exposed.method, exposed.path, wrapper) async def __on_shutdown(self, _: aiohttp.web.Application) -> None: |