Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 779
Denys Lytvinyuk Avatar asked Dec 12 '16 14:12

Denys Lytvinyuk


1 Answers

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
like image 182
Denys Lytvinyuk Avatar answered Sep 22 '22 08:09

Denys Lytvinyuk