Skip to content

Commit

Permalink
fix: Use handler's __str__() in error messages.
Browse files Browse the repository at this point in the history
Related: #
  • Loading branch information
rafalkrupinski committed Jun 18, 2024
1 parent 1d8a50a commit ccad48c
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 22 deletions.
10 changes: 0 additions & 10 deletions litestar/handlers/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,16 +254,6 @@ 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(
f"A return value of a route handler function {self.handler_fullname} should be type annotated. "
f"A return value of a route handler function {self} should be type annotated. "
"If your function doesn't return a value, annotate it as returning 'None'."
)

Expand Down
2 changes: 1 addition & 1 deletion litestar/handlers/http_handlers/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -597,7 +597,7 @@ def _validate_handler_function(self) -> None:
or is_class_and_subclass(return_annotation, ASGIFileResponse)
):
raise ImproperlyConfiguredException(
f"{self.handler_fullname}: Handlers for 'HEAD' requests must not return a value. Either return 'None' or a response type without a body."
f"{self}: Handlers for 'HEAD' requests must not return a value. Either return 'None' or a response type without a body."
)


Expand Down
14 changes: 4 additions & 10 deletions litestar/handlers/websocket_handlers/route_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,25 +85,19 @@ def _validate_handler_function(self) -> None:
super()._validate_handler_function()

if not self.parsed_fn_signature.return_type.is_subclass_of(NoneType):
raise ImproperlyConfiguredException(
f"{self.handler_fullname}: WebSocket handlers must return 'None'"
)
raise ImproperlyConfiguredException(f"{self}: WebSocket handlers must return 'None'")

if "socket" not in self.parsed_fn_signature.parameters:
raise ImproperlyConfiguredException(
f"{self.handler_fullname}: WebSocket handlers must define a 'socket' parameter"
)
raise ImproperlyConfiguredException(f"{self}: WebSocket handlers must define a 'socket' parameter")

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

if not is_async_callable(self.fn):
raise ImproperlyConfiguredException(
f"{self.handler_fullname}: WebSocket handler functions must be asynchronous"
)
raise ImproperlyConfiguredException(f"{self}: WebSocket handler functions must be asynchronous")


websocket = WebsocketRouteHandler

0 comments on commit ccad48c

Please sign in to comment.