Search code examples
pythonmiddlewarefalcon

Custom response during falcon middleware exception


I'm writing Falcon middleware for my application. When i get any errors i want to raise error, break process and return my custom response, that looks like:

{
   "status": 503,
   "message": "No Token found. Token is required."
}

But standard Falcon error implementation does not allow me to set custom fields to my response.

How to solve this problem most properly?


Solution

  • After a lot of time spent, I solved this problem in such interesting way. I put my code in a try/catch block, and when an error is caught I decided not to raise Falcon error, and just tried to write return keyword after setting response status and body, because the method is void, so it does not return anything. Now it looks like:

    resp.status = falcon.HTTP_403
    resp.body = body
    
    return