feat: raise 404 in case if package is unknown for logs and patches

This commit is contained in:
2024-01-03 03:26:16 +02:00
parent 1af04448c9
commit f1095fe007
7 changed files with 57 additions and 9 deletions

View File

@ -103,9 +103,9 @@ class LogsView(StatusViewGuard, BaseView):
try:
_, status = self.service().package_get(package_base)
logs = self.service().logs_get(package_base)
except UnknownPackageError:
raise HTTPNotFound(reason=f"Package {package_base} is unknown")
logs = self.service().logs_get(package_base)
response = {
"package_base": package_base,

View File

@ -21,6 +21,7 @@ import aiohttp_apispec # type: ignore[import-untyped]
from aiohttp.web import HTTPNoContent, HTTPNotFound, Response, json_response
from ahriman.core.exceptions import UnknownPackageError
from ahriman.models.user_access import UserAccess
from ahriman.web.schemas import AuthSchema, ErrorSchema, PatchNameSchema, PatchSchema
from ahriman.web.views.base import BaseView
@ -75,7 +76,7 @@ class PatchView(StatusViewGuard, BaseView):
200: {"description": "Success response", "schema": PatchSchema},
401: {"description": "Authorization required", "schema": ErrorSchema},
403: {"description": "Access is forbidden", "schema": ErrorSchema},
404: {"description": "Patch name is unknown", "schema": ErrorSchema},
404: {"description": "Package base and/or patch name are unknown", "schema": ErrorSchema},
500: {"description": "Internal server error", "schema": ErrorSchema},
},
security=[{"token": [GET_PERMISSION]}],
@ -95,10 +96,13 @@ class PatchView(StatusViewGuard, BaseView):
package_base = self.request.match_info["package"]
variable = self.request.match_info["patch"]
patches = self.service().patches_get(package_base, variable)
selected = next((patch for patch in patches if patch.key == variable), None)
try:
patches = self.service().patches_get(package_base, variable)
except UnknownPackageError:
raise HTTPNotFound(reason=f"Package {package_base} is unknown")
selected = next((patch for patch in patches if patch.key == variable), None)
if selected is None:
raise HTTPNotFound
raise HTTPNotFound(reason=f"Patch {variable} is unknown")
return json_response(selected.view())

View File

@ -19,8 +19,9 @@
#
import aiohttp_apispec # type: ignore[import-untyped]
from aiohttp.web import HTTPBadRequest, HTTPNoContent, Response, json_response
from aiohttp.web import HTTPBadRequest, HTTPNoContent, HTTPNotFound, Response, json_response
from ahriman.core.exceptions import UnknownPackageError
from ahriman.models.pkgbuild_patch import PkgbuildPatch
from ahriman.models.user_access import UserAccess
from ahriman.web.schemas import AuthSchema, ErrorSchema, PackageNameSchema, PatchSchema
@ -49,6 +50,7 @@ class PatchesView(StatusViewGuard, BaseView):
200: {"description": "Success response", "schema": PatchSchema(many=True)},
401: {"description": "Authorization required", "schema": ErrorSchema},
403: {"description": "Access is forbidden", "schema": ErrorSchema},
404: {"description": "Package base is unknown", "schema": ErrorSchema},
500: {"description": "Internal server error", "schema": ErrorSchema},
},
security=[{"token": [GET_PERMISSION]}],
@ -66,7 +68,10 @@ class PatchesView(StatusViewGuard, BaseView):
HTTPNotFound: if package base is unknown
"""
package_base = self.request.match_info["package"]
patches = self.service().patches_get(package_base, None)
try:
patches = self.service().patches_get(package_base, None)
except UnknownPackageError:
raise HTTPNotFound(reason=f"Package {package_base} is unknown")
response = [patch.view() for patch in patches]
return json_response(response)

View File

@ -19,8 +19,9 @@
#
import aiohttp_apispec # type: ignore[import-untyped]
from aiohttp.web import Response, json_response
from aiohttp.web import HTTPNotFound, Response, json_response
from ahriman.core.exceptions import UnknownPackageError
from ahriman.models.user_access import UserAccess
from ahriman.web.schemas import AuthSchema, ErrorSchema, LogSchema, PackageNameSchema, PaginationSchema
from ahriman.web.views.base import BaseView
@ -67,7 +68,10 @@ class LogsView(StatusViewGuard, BaseView):
"""
package_base = self.request.match_info["package"]
limit, offset = self.page()
logs = self.service().logs_get(package_base, limit, offset)
try:
logs = self.service().logs_get(package_base, limit, offset)
except UnknownPackageError:
raise HTTPNotFound(reason=f"Package {package_base} is unknown")
response = [
{