aiohttp api error handler framework

3.6k Views Asked by At

we have developed some of the aiohttp server side apis and from that api we am calling one of the python class, where i have done all business logic.

now we want to create a error handling framework for all apis, please give some ideas to implement that framework and i need to do request parameters validations as well, should i consolidate and send back all error at one time or just check one parameter send back the error message to caller.

api look like this:

 async def new_user(request):
try:
    # happy path where name is set
    user = request.query['name']
    # Process our new user
    print("Creating new user with name: " , user)

    response_obj = { 'status' : 'success' }
    # return a success json response with status code 200 i.e. 'OK'
    return web.Response(text=json.dumps(response_obj), status=200)
except Exception as e:
    # Bad path where name is not set
    response_obj = { 'status' : 'failed', 'reason': str(e), 'code' : 400 }
    # return failed with a status code of 500 i.e. 'Server Error'
    return web.Response(text=json.dumps(response_obj), status=400)
1

There are 1 best solutions below

0
On

If you are using aio-http try to create aiohttp.web.middleware.

https://docs.aiohttp.org/en/stable/web_advanced.html#middlewares