mirror of
https://github.com/mofeng-git/One-KVM.git
synced 2025-12-13 01:30:31 +08:00
refactoring
This commit is contained in:
parent
0ead2f45cf
commit
ef8b6cfda5
@ -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,
|
||||
|
||||
@ -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:
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user