kopia lustrzana https://github.com/cirospaciari/socketify.py
fix on shutdown and adds app.on_error
rodzic
5e8fbafc77
commit
ce75cbb871
|
@ -6,6 +6,7 @@ class App:
|
||||||
|
|
||||||
def on_start(self, method: callable):
|
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 router(self, prefix: str="", *middlewares):
|
||||||
def register(self, extension):
|
def register(self, extension):
|
||||||
def template(self, template_engine):
|
def template(self, template_engine):
|
||||||
|
|
|
@ -246,7 +246,8 @@ Both `app.on_start` and `app.on_shutdown` can be sync or async.
|
||||||
```python
|
```python
|
||||||
from socketify import App
|
from socketify import App
|
||||||
|
|
||||||
def run(app: App)
|
def run(app: App):
|
||||||
|
|
||||||
@app.on_start
|
@app.on_start
|
||||||
async def on_start():
|
async def on_start():
|
||||||
print("wait...")
|
print("wait...")
|
||||||
|
@ -265,5 +266,35 @@ def run(app: App)
|
||||||
def home(res, req):
|
def home(res, req):
|
||||||
res.send("Hello, World!")
|
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)
|
### Next [Upload and Post](upload-post.md)
|
|
@ -9,11 +9,12 @@ def xablau(res, req):
|
||||||
|
|
||||||
|
|
||||||
async def async_xablau(res, req):
|
async def async_xablau(res, req):
|
||||||
await asyncio.sleep(1)
|
|
||||||
raise RuntimeError("Async Xablau!")
|
raise RuntimeError("Async Xablau!")
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# this can be async no problems
|
# this can be async no problems
|
||||||
|
@app.on_error
|
||||||
def on_error(error, res, req):
|
def on_error(error, res, req):
|
||||||
# here you can log properly the error and do a pretty response to your clients
|
# here you can log properly the error and do a pretty response to your clients
|
||||||
print("Somethind goes %s" % str(error))
|
print("Somethind goes %s" % str(error))
|
||||||
|
@ -27,7 +28,8 @@ def on_error(error, res, req):
|
||||||
app.get("/", xablau)
|
app.get("/", xablau)
|
||||||
app.get("/async", async_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(
|
app.listen(
|
||||||
3000,
|
3000,
|
||||||
|
|
|
@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
|
||||||
|
|
||||||
[project]
|
[project]
|
||||||
name = "socketify"
|
name = "socketify"
|
||||||
version = "0.0.5"
|
version = "0.0.6"
|
||||||
authors = [
|
authors = [
|
||||||
{ name="Ciro Spaciari", email="ciro.spaciari@gmail.com" },
|
{ 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(
|
setuptools.setup(
|
||||||
name="socketify",
|
name="socketify",
|
||||||
version="0.0.5",
|
version="0.0.6",
|
||||||
platforms=["any"],
|
platforms=["any"],
|
||||||
author="Ciro Spaciari",
|
author="Ciro Spaciari",
|
||||||
author_email="ciro.spaciari@gmail.com",
|
author_email="ciro.spaciari@gmail.com",
|
||||||
|
|
|
@ -2561,6 +2561,7 @@ class App:
|
||||||
self._response_extension = None
|
self._response_extension = None
|
||||||
self._ws_extension = None
|
self._ws_extension = None
|
||||||
self._on_start_handler = None
|
self._on_start_handler = None
|
||||||
|
self._on_shutdown_handler = None
|
||||||
|
|
||||||
def on_start(self, method: callable):
|
def on_start(self, method: callable):
|
||||||
self._on_start_handler = method
|
self._on_start_handler = method
|
||||||
|
@ -3253,6 +3254,10 @@ class App:
|
||||||
self.loop.stop()
|
self.loop.stop()
|
||||||
return self
|
return self
|
||||||
|
|
||||||
|
def on_error(self, handler):
|
||||||
|
self.set_error_handler(handler)
|
||||||
|
return handler
|
||||||
|
|
||||||
def set_error_handler(self, handler):
|
def set_error_handler(self, handler):
|
||||||
if hasattr(handler, "__call__"):
|
if hasattr(handler, "__call__"):
|
||||||
self.error_handler = handler
|
self.error_handler = handler
|
||||||
|
@ -3271,6 +3276,7 @@ class App:
|
||||||
else:
|
else:
|
||||||
try:
|
try:
|
||||||
if inspect.iscoroutinefunction(self.error_handler):
|
if inspect.iscoroutinefunction(self.error_handler):
|
||||||
|
print("coroutine!", error)
|
||||||
self.run_async(
|
self.run_async(
|
||||||
self.error_handler(error, response, request), response
|
self.error_handler(error, response, request), response
|
||||||
)
|
)
|
||||||
|
|
Ładowanie…
Reference in New Issue