-
Notifications
You must be signed in to change notification settings - Fork 1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
allow middleware to catch any exception #1884
Comments
for you perhaps, but as i mentioned the underlying issue is handling exceptions from third party libraries which makes the current interface more than unwieldy, and effectively requires reimplementing the middleware mechanisms on chalice for developer experience (app exceptions bubble). the native lambda interface handles that well.. chalice not so much atm unless every app reimplements redundant top level handlers to rethrow everything as exception subclasses |
|
I am also looking for a solution for this issue. Here is my current objective. I will be submitting a PR shortly if all seems right. The approach proposed is similar to the solution that already exists for other framework (ie Flask) and is trying to stay as close to the existing design of Chalice. ProblemWhile the Goals
As existing middlewares can already successfully intercept all exceptions for other events, and SpecificationA new Similar to the way middlewares can be registered, we will be able to register the error handler in app or in a blueprint with either a decorator or # Decorator
@app.errorhandler(MyCustomError)
def my_handler(e: MyCustomError):
return Response(body="", status_code:400)
# Register
app.register_error_handler(MyCustomError, my_handler)
# Blueprint
@my_blueprint.errorhandler(MyCustomError)
def my_handler(e: MyCustomError):
return Response(body="", status_code:400) In the examples above, any http endpoint raising If, however, the return value isn't a @app.errorhandler(MyCustomError)
def my_handler(e: MyCustomError):
return "Not a Response object" Will return
|
Hey there any movement on this? |
Kind of a hacky way around around this, but you have to add a decorator to all routes. def error_wrapper(fn):
def wrapper():
try:
return fn()
except Exception as ex:
print('Error wrapper')
print(str(ex))
return Response(
{'result': 'error', 'error': 'server_error'},
status_code=500,
)
return wrapper
@app.route('/api/something', methods=['GET'])
@error_wrapper
def do_something():
raise ValueError('a fake error.') Or instead of decorating each route, if you like to live dangerously: def wrap_routes(app):
for path, views in app.routes.items():
for view in list(views.values()):
print(f'Wrapping route {view.method} {path}, fn: {view.view_function.__name__}')
view.view_function = error_wrapper(view.view_function)
wrap_routes(app) |
as part of #1541 and #1549 middleware support for catching exceptions was added, but the interface to it is unwieldy as it requires all exceptions to subclass a specific base exception .. given use of any library code or larger applications this is rather difficult to accomodate in practice. Given we have a default global error handler that does the exact same thing as the view response, i think uncaught exceptions (minus chalice view error) should propagate by default so middleware has a chance to handle it, and the global error acts as a fallback.
The text was updated successfully, but these errors were encountered: