Skip to content

Commit

Permalink
Feat/logging (#321)
Browse files Browse the repository at this point in the history
* Fix pyproject.toml
* Add debug to show stack trace on middleware
* Fix exception logic
* Update internal Lilya
  • Loading branch information
tarsil committed May 9, 2024
1 parent 978dc4e commit 16b57dd
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 5 deletions.
6 changes: 5 additions & 1 deletion esmerald/applications.py
Original file line number Diff line number Diff line change
Expand Up @@ -2391,7 +2391,11 @@ def build_middleware_stack(self) -> "ASGIApp":
handlers=exception_handlers,
debug=debug,
),
DefineMiddleware(AsyncExitStackMiddleware, config=self.async_exit_config),
DefineMiddleware(
AsyncExitStackMiddleware,
config=self.async_exit_config,
debug=debug,
),
]
)

Expand Down
1 change: 1 addition & 0 deletions esmerald/exception_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ async def http_exception_handler(
"""
Default exception handler for LilyaHTTPException and Esmerald HTTPException.
"""

extra = getattr(exc, "extra", None)
headers = getattr(exc, "headers", None)

Expand Down
14 changes: 13 additions & 1 deletion esmerald/middleware/asyncexitstack.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import traceback
from contextlib import AsyncExitStack
from typing import Optional

Expand All @@ -8,16 +9,23 @@


class AsyncExitStackMiddleware(MiddlewareProtocol):
def __init__(self, app: "ASGIApp", config: "AsyncExitConfig"):
def __init__(
self,
app: "ASGIApp",
config: "AsyncExitConfig",
debug: bool = False,
):
"""AsyncExitStack Middleware class.
Args:
app: The 'next' ASGI app to call.
config: The AsyncExitConfig instance.
debug: If the application should print the stack trace on any error.
"""
super().__init__(app)
self.app = app
self.config = config
self.debug = debug

async def __call__(self, scope: "Scope", receive: "Receive", send: "Send") -> None:
if not AsyncExitStack:
Expand All @@ -30,5 +38,9 @@ async def __call__(self, scope: "Scope", receive: "Receive", send: "Send") -> No
await self.app(scope, receive, send)
except Exception as e:
exception = e

if exception and self.debug:
traceback.print_exception(exception, exception, exception.__traceback__) # type: ignore

if exception:
raise exception
2 changes: 2 additions & 0 deletions esmerald/routing/apis/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -311,9 +311,11 @@ def get_route_handlers(

if self.exception_handlers:
route_handler.exception_handlers = self.get_exception_handlers(route_handler) # type: ignore

if self.tags or []: # pragma: no cover
for tag in reversed(self.tags):
route_handler.tags.insert(0, tag)

route_handlers.append(route_handler)

return route_handlers
Expand Down
3 changes: 1 addition & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ dependencies = [
"click>=8.1.4,<9.0.0",
"itsdangerous>=2.1.2,<3.0.0",
"jinja2>=3.1.2,<4.0.0",
"lilya>=0.3.3",
"lilya>=0.7.1",
"loguru>=0.7.0,<0.8.0",
"pydantic>=2.5.3,<3.0.0",
"pydantic-settings>=2.0.0,<3.0.0",
Expand Down Expand Up @@ -167,7 +167,6 @@ test = "ESMERALD_SETTINGS_MODULE='tests.settings.TestSettings' pytest {args}"
coverage = "ESMERALD_SETTINGS_MODULE='tests.settings.TestSettings' pytest --cov=asyncz --cov=tests --cov-report=term-missing:skip-covered --cov-report=html tests {args}"
check_types = "mypy -p esmerald"


[tool.hatch.envs.hatch-test]
features = ["all"]
template = "test"
Expand Down
1 change: 0 additions & 1 deletion tests/routing/test_routing.py
Original file line number Diff line number Diff line change
Expand Up @@ -970,7 +970,6 @@ def exc(request: Request) -> CustomException:
client = test_app_client_factory(app)
response = client.get("/sub/")

assert "Exception: Exc" in response.text
assert response.status_code == 500
assert response.reason_phrase == "Internal Server Error"

Expand Down

0 comments on commit 16b57dd

Please sign in to comment.