Skip to content

Commit

Permalink
Fix: add handler name to exceptions in handler validation.
Browse files Browse the repository at this point in the history
Related: #
  • Loading branch information
rafalkrupinski committed Jun 16, 2024
1 parent 95951db commit aeaa61b
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 6 deletions.
10 changes: 10 additions & 0 deletions litestar/handlers/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,16 @@ def handler_name(self) -> str:
"""
return get_name(unwrap_partial(self.fn))

@property
def handler_fullname(self) -> str:
"""Get the qualified name of the handler function.
Returns:
Qualified name of the handler function
"""
handler_fn = unwrap_partial(self.fn)
return handler_fn.__module__ + "." + handler_fn.__qualname__

@property
def dependency_name_set(self) -> set[str]:
"""Set of all dependency names provided in the handler's ownership layers."""
Expand Down
2 changes: 1 addition & 1 deletion litestar/handlers/http_handlers/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -574,7 +574,7 @@ def _validate_handler_function(self) -> None:

if return_type.annotation is Empty:
raise ImproperlyConfiguredException(
"A return value of a route handler function should be type annotated. "
f"A return value of a route handler function {self.handler_fullname} should be type annotated. "
"If your function doesn't return a value, annotate it as returning 'None'."
)

Expand Down
4 changes: 3 additions & 1 deletion litestar/handlers/http_handlers/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -588,7 +588,9 @@ def _validate_handler_function(self) -> None:
or is_class_and_subclass(return_annotation, File)
or is_class_and_subclass(return_annotation, ASGIFileResponse)
):
raise ImproperlyConfiguredException("A response to a head request should not have a body")
raise ImproperlyConfiguredException(
f"A route handler function '{self.handler_fullname}' that handles HEAD request should not have a body"
)


class patch(HTTPRouteHandler, _SubclassWarningMixin):
Expand Down
16 changes: 12 additions & 4 deletions litestar/handlers/websocket_handlers/route_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,17 +85,25 @@ def _validate_handler_function(self) -> None:
super()._validate_handler_function()

if not self.parsed_fn_signature.return_type.is_subclass_of(NoneType):
raise ImproperlyConfiguredException("Websocket handler functions should return 'None'")
raise ImproperlyConfiguredException(
f"Websocket handler function '{self.handler_fullname}' should return 'None'"
)

if "socket" not in self.parsed_fn_signature.parameters:
raise ImproperlyConfiguredException("Websocket handlers must set a 'socket' kwarg")
raise ImproperlyConfiguredException(
f"Websocket handler '{self.handler_fullname}' must set a 'socket' kwarg"
)

for param in ("request", "body", "data"):
if param in self.parsed_fn_signature.parameters:
raise ImproperlyConfiguredException(f"The {param} kwarg is not supported with websocket handlers")
raise ImproperlyConfiguredException(
f"{self.handler_fullname}: the {param} kwarg is not supported with websocket handlers"
)

if not is_async_callable(self.fn):
raise ImproperlyConfiguredException("Functions decorated with 'websocket' must be async functions")
raise ImproperlyConfiguredException(
f"Function '{self.handler_fullname}' decorated with 'websocket' must be async functions"
)


websocket = WebsocketRouteHandler

0 comments on commit aeaa61b

Please sign in to comment.