kopia lustrzana https://github.com/cirospaciari/socketify.py
fix on shutdown and adds app.on_error
rodzic
5e8fbafc77
commit
ce75cbb871
|
@ -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):
|
||||
|
|
|
@ -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)
|
|
@ -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,
|
||||
|
|
|
@ -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" },
|
||||
]
|
||||
|
|
2
setup.py
2
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",
|
||||
|
|
|
@ -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
|
||||
)
|
||||
|
|
Ładowanie…
Reference in New Issue