Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions components/renku_data_services/base_api/error_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from sqlite3 import Error as SqliteError
from typing import Any, Optional, Protocol, TypeVar, Union

import httpx
import jwt
from asyncpg import exceptions as postgres_exceptions
from pydantic import ValidationError as PydanticValidationError
Expand Down Expand Up @@ -66,9 +67,9 @@ def __init__(self, api_spec: ApiSpec, base: type[BaseRenderer] = TextRenderer) -
self.api_spec = api_spec
super().__init__(base)

def default(self, request: Request, exception: Exception) -> HTTPResponse:
"""Overrides the default error handler."""
formatted_exception = errors.BaseError()
@classmethod
def _get_formatted_exception(cls, exception: Exception) -> errors.BaseError | None:
formatted_exception: errors.BaseError | None = None
match exception:
case errors.BaseError():
formatted_exception = exception
Expand Down Expand Up @@ -132,6 +133,17 @@ def default(self, request: Request, exception: Exception) -> HTTPResponse:
)
case jwt.exceptions.InvalidTokenError():
formatted_exception = errors.InvalidTokenError()

case httpx.RequestError():
req_uri = exception.request.url
formatted_exception = errors.BaseError(message=f"Error on remote connection to {req_uri}: {exception}")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: should we also include the method here?


return formatted_exception

def default(self, request: Request, exception: Exception) -> HTTPResponse:
"""Overrides the default error handler."""
formatted_exception = self._get_formatted_exception(exception) or errors.BaseError()

self.log(request, formatted_exception)
if formatted_exception.status_code == 500 and "PYTEST_CURRENT_TEST" in os.environ:
# TODO: Figure out how to do logging properly in here, I could not get the sanic logs to show up from here
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
"""Test functions for the error_handler module."""

from sqlite3 import Error as SqliteError

import httpx
import jwt
from asyncpg.exceptions import PostgresError
from pydantic import ValidationError as PydanticValidationError
from sanic import SanicException
from sanic_ext.exceptions import ValidationError as SanicValidationError
from sqlalchemy.exc import SQLAlchemyError

from renku_data_services.base_api.error_handler import CustomErrorHandler
from renku_data_services.errors import errors


def make_sqlite_error() -> SqliteError:
err = SqliteError()
err.sqlite_errorcode = 1
err.sqlite_errorname = "name"
return err


def make_pg_error() -> PostgresError:
err = PostgresError()
err.msg = "not supported"
err.pgcode = "A0110"
return err


def test_match_exception() -> None:
expect_to_match = [
errors.BaseError(),
SanicValidationError(extra={"exception": PydanticValidationError("oops", [])}),
SanicValidationError(extra={"exception": TypeError("oops")}),
PydanticValidationError("oops", []),
SanicException(),
make_sqlite_error(),
make_pg_error(),
SQLAlchemyError(),
OverflowError(),
jwt.exceptions.InvalidTokenError(),
httpx.ConnectError("oops"),
httpx.UnsupportedProtocol("ftp"),
]

for exc in expect_to_match:
result = CustomErrorHandler._get_formatted_exception(exc)
assert result is not None
Loading