Search code examples
pythonflask

Is there a widely accepted way to structure a Flask route with many points of failure?


I'm working on a Flask application with a route that performs extensive processing of data before a response is returned to the user. Each step of this data processing has its own function, and each function is a potential point of failure. Each function returns some piece of data that is used to either build the response, or is used in the next function. In the event of a failure, the route should return a proper error message and status code based on where the failure occurred.

My first, naive approach was to individually wrap each function call in a try-except block, then return an error message and status code if an error was raised. This resulted in a wall of try-except blocks that worked, but looked poorly developed.

Is there a widely accepted structure or best practice for routes that have many potential points of failure?


Solution

  • Hope this help you: sample structure.

    In app, you can use register_error_handler to apply a handle exception before responding to user without use try-except.

    See this file, It help you to format error response