From ce75cbb871325ad4c6b1ac0ef136df160bc79919 Mon Sep 17 00:00:00 2001 From: Ciro Date: Mon, 9 Jan 2023 16:51:27 -0300 Subject: [PATCH] fix on shutdown and adds app.on_error --- docs/api.md | 3 ++- docs/basics.md | 33 ++++++++++++++++++++++++++++++++- examples/error_handler.py | 6 ++++-- pyproject.toml | 2 +- setup.py | 2 +- src/socketify/socketify.py | 6 ++++++ 6 files changed, 46 insertions(+), 6 deletions(-) diff --git a/docs/api.md b/docs/api.md index 7122835..2bb0db0 100644 --- a/docs/api.md +++ b/docs/api.md @@ -5,7 +5,8 @@ class App: def __init__(self, options=None, request_response_factory_max_items=0, websocket_factory_max_items=0, task_factory_max_items=100_000, lifespan=True): def on_start(self, method: callable): - def on_shutdown(self, method: callable): + def on_shutdown(self, method: callable): + def on_error(self, method: callable): def router(self, prefix: str="", *middlewares): def register(self, extension): def template(self, template_engine): diff --git a/docs/basics.md b/docs/basics.md index 1c2d692..9324baa 100644 --- a/docs/basics.md +++ b/docs/basics.md @@ -246,7 +246,8 @@ Both `app.on_start` and `app.on_shutdown` can be sync or async. ```python from socketify import App -def run(app: App) +def run(app: App): + @app.on_start async def on_start(): print("wait...") @@ -265,5 +266,35 @@ def run(app: App) def home(res, req): res.send("Hello, World!") +``` + +# Error handler events +You can set a error handler to give the user an custom 500 page and/or for logging properly + +Using `app.set_error_handler(on_error)` or `app.on_error` decorator. + + +```python +from socketify import App + +def run(app: App): + + @app.on_error + def on_error(error, res, req): + # here you can log properly the error and do a pretty response to your clients + print("Somethind goes %s" % str(error)) + # response and request can be None if the error is in an async function + if res != None: + # if response exists try to send something + res.write_status(500) + res.end("Sorry we did something wrong") + + router = app.router() + + @router.get("/") + def home(res, req): + raise RuntimeError("Oops!") + + ``` ### Next [Upload and Post](upload-post.md) \ No newline at end of file diff --git a/examples/error_handler.py b/examples/error_handler.py index 71b7936..5f58b98 100644 --- a/examples/error_handler.py +++ b/examples/error_handler.py @@ -9,11 +9,12 @@ def xablau(res, req): async def async_xablau(res, req): - await asyncio.sleep(1) raise RuntimeError("Async Xablau!") + # this can be async no problems +@app.on_error def on_error(error, res, req): # here you can log properly the error and do a pretty response to your clients print("Somethind goes %s" % str(error)) @@ -27,7 +28,8 @@ def on_error(error, res, req): app.get("/", xablau) app.get("/async", async_xablau) -app.set_error_handler(on_error) +# you can also use set_error_handler +# app.set_error_handler(on_error) app.listen( 3000, diff --git a/pyproject.toml b/pyproject.toml index 68d856f..e3de7d3 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta" [project] name = "socketify" -version = "0.0.5" +version = "0.0.6" authors = [ { name="Ciro Spaciari", email="ciro.spaciari@gmail.com" }, ] diff --git a/setup.py b/setup.py index b2e06de..5808f8a 100644 --- a/setup.py +++ b/setup.py @@ -58,7 +58,7 @@ with open("README.md", "r", encoding="utf-8") as fh: setuptools.setup( name="socketify", - version="0.0.5", + version="0.0.6", platforms=["any"], author="Ciro Spaciari", author_email="ciro.spaciari@gmail.com", diff --git a/src/socketify/socketify.py b/src/socketify/socketify.py index 46bc521..3603f91 100644 --- a/src/socketify/socketify.py +++ b/src/socketify/socketify.py @@ -2561,6 +2561,7 @@ class App: self._response_extension = None self._ws_extension = None self._on_start_handler = None + self._on_shutdown_handler = None def on_start(self, method: callable): self._on_start_handler = method @@ -3253,6 +3254,10 @@ class App: self.loop.stop() return self + def on_error(self, handler): + self.set_error_handler(handler) + return handler + def set_error_handler(self, handler): if hasattr(handler, "__call__"): self.error_handler = handler @@ -3271,6 +3276,7 @@ class App: else: try: if inspect.iscoroutinefunction(self.error_handler): + print("coroutine!", error) self.run_async( self.error_handler(error, response, request), response )