2022-05-31 20:53:20 +00:00
|
|
|
from socketify import App, AppOptions, AppListenOptions
|
|
|
|
import asyncio
|
|
|
|
|
|
|
|
app = App()
|
|
|
|
|
2022-11-16 19:28:46 +00:00
|
|
|
|
2022-05-31 20:53:20 +00:00
|
|
|
def xablau(res, req):
|
|
|
|
raise RuntimeError("Xablau!")
|
|
|
|
|
2022-11-16 19:28:46 +00:00
|
|
|
|
2022-05-31 20:53:20 +00:00
|
|
|
async def async_xablau(res, req):
|
|
|
|
raise RuntimeError("Async Xablau!")
|
|
|
|
|
2022-11-16 19:28:46 +00:00
|
|
|
|
2023-01-09 19:51:27 +00:00
|
|
|
|
2022-11-16 19:28:46 +00:00
|
|
|
# this can be async no problems
|
2023-01-09 19:51:27 +00:00
|
|
|
@app.on_error
|
2022-11-16 19:28:46 +00:00
|
|
|
def on_error(error, res, req):
|
|
|
|
# here you can log properly the error and do a pretty response to your clients
|
2022-05-31 20:53:20 +00:00
|
|
|
print("Somethind goes %s" % str(error))
|
2022-11-16 19:28:46 +00:00
|
|
|
# response and request can be None if the error is in an async function
|
2022-05-31 20:53:20 +00:00
|
|
|
if res != None:
|
2022-11-16 19:28:46 +00:00
|
|
|
# if response exists try to send something
|
2022-05-31 20:53:20 +00:00
|
|
|
res.write_status(500)
|
|
|
|
res.end("Sorry we did something wrong")
|
|
|
|
|
2022-11-16 19:28:46 +00:00
|
|
|
|
2022-05-31 20:53:20 +00:00
|
|
|
app.get("/", xablau)
|
|
|
|
app.get("/async", async_xablau)
|
|
|
|
|
2023-01-09 19:51:27 +00:00
|
|
|
# you can also use set_error_handler
|
|
|
|
# app.set_error_handler(on_error)
|
2022-05-31 20:53:20 +00:00
|
|
|
|
2022-11-16 19:28:46 +00:00
|
|
|
app.listen(
|
|
|
|
3000,
|
|
|
|
lambda config: print(
|
|
|
|
"Listening on port http://localhost:%s now\n" % str(config.port)
|
|
|
|
),
|
|
|
|
)
|
|
|
|
app.run()
|