Handling Errors

Batman learned how to handle the error for different exception in his application. He wrote the following code to handle exceptions :

Request

GET
/hello_world/{item_id}
from robyn import Robyn, HTTPException, status_codes

app = Robyn(__file__)

items = {"foo": "The Foo Wrestlers"}

@app.get("/hello_world/{item_id}")
async def read_item(request, item_id):
    if item_id not in items:
        raise HTTPException(status_code=status_codes.HTTP_404_NOT_FOUND, detail="Item not found")
    return {"item": items[item_id]}

Custom Exception Handler

Batman learned how to create custom error handlers for different exception types in his application. He wrote the following code to handle exceptions and return a custom error response:

Request

GET
/hello_world
@app.exception
def handle_exception(error):
    return Response({status_code=500, body=f"error msg: {error}", headers={}})

What's next?

Now, Batman wanted to scale his application across multiple cores. Robyn led him to Scaling.