Difference between Flask abort() or returning a status

Viewed 5352

What is the difference between abort(400) and returning a response with a 400 status? Is using return bad form for errors?

abort(400, "some error message")
# or
return {'message': "some error message"}, 400
2 Answers

abort raises an error, which an error handler will convert to a response. return returns a response, error handlers don't trigger. It's up to how you want your application to flow.

abort is a wrapper that raises HTTPException classes. Calling abort is the same as raising an exception and Flask will handle both the same way. Returning is not the same as raising an exception and will be handled differently.

Related