Skip to content

Commit da8d003

Browse files
committed
fixed exception logging and added tuple suppose for route response
1 parent ed013c1 commit da8d003

File tree

4 files changed

+17
-32
lines changed

4 files changed

+17
-32
lines changed

ninja_extra/controllers/route/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ def __init__(
7474
_response_computed.update({item.status_code: item.get_schema()})
7575
elif isinstance(item, dict):
7676
_response_computed.update(item)
77+
elif isinstance(item, tuple):
78+
_response_computed.update({item[0]: item[1]})
7779
if not _response_computed:
7880
raise RouteInvalidParameterException(
7981
f"Invalid response configuration: {response}"

ninja_extra/operation.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
from django.http import HttpRequest
1515
from django.http.response import HttpResponse, HttpResponseBase
16+
from django.utils.encoding import force_str
1617
from ninja.constants import NOT_SET
1718
from ninja.operation import (
1819
AsyncOperation as NinjaAsyncOperation,
@@ -21,6 +22,7 @@
2122
)
2223
from ninja.signature import is_async
2324

25+
from ninja_extra.exceptions import APIException
2426
from ninja_extra.logger import request_logger
2527
from ninja_extra.signals import route_context_finished, route_context_started
2628

@@ -47,11 +49,18 @@ def _log_action(
4749
msg = (
4850
f'"{request.method.upper() if request.method else "METHOD NOT FOUND"} - '
4951
f'{route_function.controller.__name__}[{self.view_func.__name__}] {request.path}" '
50-
f"{duration if duration else str(ex)}"
52+
f"{duration if duration else ''}"
5153
)
54+
if ex:
55+
msg += (
56+
f"{ex.status_code}"
57+
if isinstance(ex, APIException)
58+
else f"{force_str(ex.args)}"
59+
)
60+
5261
logger(msg, **kwargs)
53-
except (Exception,):
54-
pass
62+
except (Exception,) as dd:
63+
print(dd)
5564

5665
def get_execution_context(
5766
self, request: HttpRequest, *args: Any, **kwargs: Any

tests/test_controller.py

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from ninja_extra import APIController, NinjaExtraAPI, route, router
88
from ninja_extra.controllers import RouteFunction
99
from ninja_extra.controllers.base import MissingRouterDecoratorException
10-
from ninja_extra.controllers.response import ControllerResponse, Detail, Id, Ok
10+
from ninja_extra.controllers.response import Detail, Id, Ok
1111
from ninja_extra.controllers.router import ControllerRouter
1212
from ninja_extra.permissions.common import AllowAny
1313

@@ -100,25 +100,6 @@ def test_controller_route_definition_should_return_instance_route_definitions(se
100100

101101

102102
class TestAPIControllerResponse:
103-
class CustomResponse(ControllerResponse):
104-
status_code = 502
105-
106-
def __init__(self, email: str, name: str):
107-
super().__init__()
108-
self.email = email
109-
self.name = name
110-
111-
class CustomSchema(Schema):
112-
email: str
113-
name: str
114-
115-
@classmethod
116-
def get_schema(cls) -> Union[Schema, Type[Schema], Any]:
117-
return cls.CustomSchema
118-
119-
def convert_to_schema(self) -> Any:
120-
return self.CustomSchema.from_orm(self)
121-
122103
ok_response = Ok("OK")
123104
id_response = Id("ID")
124105
detail_response = Detail(dict(errors=[dict(test="passed")]), status_code=302)
@@ -138,13 +119,6 @@ def test_controller_response(self):
138119
message=dict(errors=[dict(test="passed")])
139120
)
140121
assert self.id_response.status_code != Detail.status_code
141-
# CustomResponse Response
142-
custom_response = self.CustomResponse(email="some_email", name="some_name")
143-
assert custom_response.get_schema() == self.CustomResponse.get_schema()
144-
assert custom_response.convert_to_schema() == self.CustomResponse.get_schema()(
145-
email="some_email", name="some_name"
146-
)
147-
assert custom_response.status_code == self.CustomResponse.status_code
148122

149123
def test_controller_response_works(self):
150124
detail = Detail("5242", status_code=302)

tests/test_route.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ def example_list_create(self, ex_id: str):
126126
assert "Invalid response configuration" in str(ex)
127127

128128
def test_route_response_parameters_computed_correctly(self):
129-
unique_response = [Ok, Id, {302: Schema}]
129+
unique_response = [Ok, Id, {302: Schema}, (401, Schema)]
130130
non_unique_response = [
131131
Ok,
132132
Id,
@@ -141,7 +141,7 @@ def example_unique_response(self, ex_id: str):
141141
def example_non_unique_response(self, ex_id: str):
142142
pass
143143

144-
assert len(example_unique_response.route.route_params.response) == 3
144+
assert len(example_unique_response.route.route_params.response) == 4
145145
assert len(example_non_unique_response.route.route_params.response) == 2
146146

147147
@pytest.mark.parametrize(

0 commit comments

Comments
 (0)