Skip to content

Commit

Permalink
refactor: remove temporary from constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
janbritz committed Nov 19, 2024
1 parent 1ca16cd commit 90d2eae
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 13 deletions.
11 changes: 6 additions & 5 deletions questionpy_server/web/_decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ async def wrapper(request: web.Request, *args: _P.args, **kwargs: _P.kwargs) ->
kwargs[param.name] = parts.question_state
elif param.default is Parameter.empty:
_msg = "A question state part is required but was not provided."
raise InvalidRequestError(reason=_msg, temporary=False)
raise InvalidRequestError(reason=_msg)

return await handler(request, *args, **kwargs)

Expand Down Expand Up @@ -132,7 +132,7 @@ async def wrapper(request: web.Request, *args: _P.args, **kwargs: _P.kwargs) ->

if parts.main is None:
_msg = "The main body is required but was not provided."
raise InvalidRequestError(reason=_msg, temporary=False)
raise InvalidRequestError(reason=_msg)

kwargs[param.name] = _validate_from_http(parts.main, param.annotation)
return await handler(request, *args, **kwargs)
Expand All @@ -151,7 +151,7 @@ async def _get_package_from_request(request: web.Request) -> Package:
f"The request URI specifies a package with hash '{uri_package_hash}', but the sent package has a hash of"
f" '{parts.package.hash}'."
)
raise InvalidPackageError(reason=msg, temporary=False)
raise InvalidPackageError(reason=msg)

package = None
if uri_package_hash:
Expand All @@ -171,7 +171,7 @@ async def _get_package_from_request(request: web.Request) -> Package:
)
raise PackageNotFoundError(reason=msg, temporary=False)
msg = "The package is required but was not provided."
raise InvalidRequestError(reason=msg, temporary=False)
raise InvalidRequestError(reason=msg)

return package

Expand Down Expand Up @@ -285,4 +285,5 @@ def _validate_from_http(raw_body: str | bytes, param_class: type[_M]) -> _M:
try:
return param_class.model_validate_json(raw_body)
except ValidationError as error:
raise InvalidRequestError(reason="Invalid JSON body", temporary=False) from error
msg = "Invalid JSON body"
raise InvalidRequestError(reason=msg) from error
18 changes: 10 additions & 8 deletions questionpy_server/web/errors.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# This file is part of the QuestionPy Server. (https://questionpy.org)
# The QuestionPy Server is free software released under terms of the MIT license. See LICENSE.md.
# (c) Technische Universität Berlin, innoCampus <[email protected]>
from typing import Any

from aiohttp import web
from aiohttp.log import web_logger

Expand Down Expand Up @@ -46,49 +48,49 @@ def __init__(self, *, reason: str | None, temporary: bool) -> None:


class InvalidAttemptStateError(web.HTTPBadRequest, _ExceptionMixin):
def __init__(self, *, reason: str | None, temporary: bool) -> None:
def __init__(self, *, reason: str | None, **_: Any) -> None:
super().__init__(
"Invalid attempt state was provided",
RequestError(
error_code=RequestErrorCode.INVALID_ATTEMPT_STATE,
reason=reason,
temporary=temporary,
temporary=False,
),
)


class InvalidQuestionStateError(web.HTTPBadRequest, _ExceptionMixin):
def __init__(self, *, reason: str | None, temporary: bool) -> None:
def __init__(self, *, reason: str | None, **_: Any) -> None:
super().__init__(
"Invalid question state was provided",
RequestError(
error_code=RequestErrorCode.INVALID_QUESTION_STATE,
reason=reason,
temporary=temporary,
temporary=False,
),
)


class InvalidPackageError(web.HTTPBadRequest, _ExceptionMixin):
def __init__(self, *, reason: str | None, temporary: bool) -> None:
def __init__(self, *, reason: str | None, **_: Any) -> None:
super().__init__(
"Invalid package was provided",
RequestError(
error_code=RequestErrorCode.INVALID_PACKAGE,
reason=reason,
temporary=temporary,
temporary=False,
),
)


class InvalidRequestError(web.HTTPUnprocessableEntity, _ExceptionMixin):
def __init__(self, *, reason: str | None, temporary: bool) -> None:
def __init__(self, *, reason: str | None, **_: Any) -> None:
super().__init__(
"Invalid request body was provided",
RequestError(
error_code=RequestErrorCode.INVALID_REQUEST,
reason=reason,
temporary=temporary,
temporary=False,
),
)

Expand Down

0 comments on commit 90d2eae

Please sign in to comment.