always return json in responses

This commit is contained in:
2021-10-20 02:12:39 +03:00
parent be017ed102
commit d8523bd83b
8 changed files with 62 additions and 36 deletions

View File

@ -18,8 +18,8 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
from aiohttp.web import middleware, Request
from aiohttp.web_exceptions import HTTPException
from aiohttp.web_response import StreamResponse
from aiohttp.web_exceptions import HTTPClientError, HTTPException, HTTPServerError
from aiohttp.web_response import json_response, StreamResponse
from logging import Logger
from ahriman.web.middlewares import HandlerType, MiddlewareType
@ -35,10 +35,15 @@ def exception_handler(logger: Logger) -> MiddlewareType:
async def handle(request: Request, handler: HandlerType) -> StreamResponse:
try:
return await handler(request)
except HTTPClientError as e:
return json_response(data={"error": e.reason}, status=e.status_code)
except HTTPServerError as e:
logger.exception("server exception during performing request to %s", request.path)
return json_response(data={"error": e.reason}, status=e.status_code)
except HTTPException:
raise # we do not raise 5xx exceptions actually so it should be fine
except Exception:
logger.exception("exception during performing request to %s", request.path)
raise
raise # just raise 2xx and 3xx codes
except Exception as e:
logger.exception("unknown exception during performing request to %s", request.path)
return json_response(data={"error": str(e)}, status=500)
return handle

View File

@ -17,7 +17,7 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
from aiohttp.web import HTTPFound, Response, json_response
from aiohttp.web import HTTPBadRequest, HTTPFound, Response
from ahriman.models.user_access import UserAccess
from ahriman.web.views.base import BaseView
@ -42,12 +42,11 @@ class AddView(BaseView):
:return: redirect to main page on success
"""
data = await self.extract_data(["packages"])
try:
data = await self.extract_data(["packages"])
packages = data["packages"]
except Exception as e:
return json_response(data=str(e), status=400)
raise HTTPBadRequest(reason=str(e))
self.spawner.packages_add(packages, now=True)

View File

@ -17,7 +17,7 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
from aiohttp.web import HTTPFound, Response, json_response
from aiohttp.web import HTTPBadRequest, HTTPFound, Response
from ahriman.models.user_access import UserAccess
from ahriman.web.views.base import BaseView
@ -42,12 +42,11 @@ class RemoveView(BaseView):
:return: redirect to main page on success
"""
data = await self.extract_data(["packages"])
try:
data = await self.extract_data(["packages"])
packages = data["packages"]
except Exception as e:
return json_response(data=str(e), status=400)
raise HTTPBadRequest(reason=str(e))
self.spawner.packages_remove(packages)

View File

@ -17,7 +17,7 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
from aiohttp.web import HTTPFound, Response, json_response
from aiohttp.web import HTTPBadRequest, HTTPFound, Response
from ahriman.models.user_access import UserAccess
from ahriman.web.views.base import BaseView
@ -42,12 +42,11 @@ class RequestView(BaseView):
:return: redirect to main page on success
"""
data = await self.extract_data(["packages"])
try:
data = await self.extract_data(["packages"])
packages = data["packages"]
except Exception as e:
return json_response(data=str(e), status=400)
raise HTTPBadRequest(reason=str(e))
self.spawner.packages_add(packages, now=False)

View File

@ -19,7 +19,7 @@
#
import aur # type: ignore
from aiohttp.web import Response, json_response
from aiohttp.web import HTTPBadRequest, Response, json_response
from typing import Callable, Iterator
from ahriman.models.user_access import UserAccess
@ -47,7 +47,7 @@ class SearchView(BaseView):
search_string = " ".join(search)
if not search_string:
return json_response(data="Search string must not be empty", status=400)
raise HTTPBadRequest(reason="Search string must not be empty")
packages = aur.search(search_string)
comparator: Callable[[aur.Package], str] = lambda item: str(item.package_base)

View File

@ -17,7 +17,7 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
from aiohttp.web import HTTPNoContent, Response, json_response
from aiohttp.web import HTTPBadRequest, HTTPNoContent, Response, json_response
from ahriman.models.build_status import BuildStatusEnum
from ahriman.models.user_access import UserAccess
@ -53,12 +53,11 @@ class AhrimanView(BaseView):
:return: 204 on success
"""
data = await self.extract_data()
try:
data = await self.extract_data()
status = BuildStatusEnum(data["status"])
except Exception as e:
return json_response(data=str(e), status=400)
raise HTTPBadRequest(reason=str(e))
self.service.update_self(status)

View File

@ -17,7 +17,7 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
from aiohttp.web import HTTPNoContent, HTTPNotFound, Response, json_response
from aiohttp.web import HTTPBadRequest, HTTPNoContent, HTTPNotFound, Response, json_response
from ahriman.core.exceptions import UnknownPackage
from ahriman.models.build_status import BuildStatusEnum
@ -88,11 +88,11 @@ class PackageView(BaseView):
package = Package.from_json(data["package"]) if "package" in data else None
status = BuildStatusEnum(data["status"])
except Exception as e:
return json_response(data=str(e), status=400)
raise HTTPBadRequest(reason=str(e))
try:
self.service.update(base, status, package)
except UnknownPackage:
return json_response(data=f"Package {base} is unknown, but no package body set", status=400)
raise HTTPBadRequest(reason=f"Package {base} is unknown, but no package body set")
raise HTTPNoContent()