Skip to content

Commit a37c1fd

Browse files
committed
more tests
1 parent 80eea24 commit a37c1fd

File tree

3 files changed

+59
-4
lines changed

3 files changed

+59
-4
lines changed

components/renku_data_services/base_api/error_handler.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,9 @@ def __init__(self, api_spec: ApiSpec, base: type[BaseRenderer] = TextRenderer) -
6767
self.api_spec = api_spec
6868
super().__init__(base)
6969

70-
def default(self, request: Request, exception: Exception) -> HTTPResponse:
71-
"""Overrides the default error handler."""
72-
formatted_exception = errors.BaseError()
70+
@classmethod
71+
def _get_formatted_exception(cls, exception: Exception) -> errors.BaseError | None:
72+
formatted_exception: errors.BaseError | None = None
7373
match exception:
7474
case errors.BaseError():
7575
formatted_exception = exception
@@ -137,6 +137,12 @@ def default(self, request: Request, exception: Exception) -> HTTPResponse:
137137
case httpx.RequestError():
138138
formatted_exception = errors.BaseError(message=f"Error on remote connection: {exception}")
139139

140+
return formatted_exception
141+
142+
def default(self, request: Request, exception: Exception) -> HTTPResponse:
143+
"""Overrides the default error handler."""
144+
formatted_exception = self._get_formatted_exception(exception) or errors.BaseError()
145+
140146
self.log(request, formatted_exception)
141147
if formatted_exception.status_code == 500 and "PYTEST_CURRENT_TEST" in os.environ:
142148
# TODO: Figure out how to do logging properly in here, I could not get the sanic logs to show up from here
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
"""Test functions for the error_handler module."""
2+
3+
from sqlite3 import Error as SqliteError
4+
5+
import httpx
6+
import jwt
7+
from asyncpg.exceptions import PostgresError
8+
from pydantic import ValidationError as PydanticValidationError
9+
from sanic import SanicException
10+
from sanic_ext.exceptions import ValidationError as SanicValidationError
11+
from sqlalchemy.exc import SQLAlchemyError
12+
13+
from renku_data_services.base_api.error_handler import CustomErrorHandler
14+
from renku_data_services.errors import errors
15+
16+
17+
def make_sqlite_error() -> SqliteError:
18+
err = SqliteError()
19+
err.sqlite_errorcode = 1
20+
err.sqlite_errorname = "name"
21+
return err
22+
23+
24+
def make_pg_error() -> PostgresError:
25+
err = PostgresError()
26+
err.msg = "not supported"
27+
err.pgcode = "A0110"
28+
return err
29+
30+
31+
def test_match_exception() -> None:
32+
expect_to_match = [
33+
errors.BaseError(),
34+
SanicValidationError(extra={"exception": PydanticValidationError("oops", [])}),
35+
SanicValidationError(extra={"exception": TypeError("oops")}),
36+
PydanticValidationError("oops", []),
37+
SanicException(),
38+
make_sqlite_error(),
39+
make_pg_error(),
40+
SQLAlchemyError(),
41+
OverflowError(),
42+
jwt.exceptions.InvalidTokenError(),
43+
httpx.ConnectError("oops"),
44+
httpx.UnsupportedProtocol("ftp"),
45+
]
46+
47+
for exc in expect_to_match:
48+
result = CustomErrorHandler._get_formatted_exception(exc)
49+
assert result is not None

test/components/renku_data_services/utils/test_http.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ async def expect_error(rr: RunRequest, url: str) -> RequestError:
2323

2424
@pytest.mark.asyncio
2525
async def test_add_url() -> None:
26-
urls = ["http://bad-label--.com", ""]
26+
urls = ["http://bad-label--.com", "", "ftp://x.com"]
2727
methods: list[RunRequest] = [client.post, client.get, client.delete, client.patch, client.head, client.put]
2828
for m in methods:
2929
for url in urls:

0 commit comments

Comments
 (0)