Skip to content

Allow error handler to pass on decision to subsequent handlers #2197

@tuukkamustonen

Description

@tuukkamustonen

I would like to do error handling conditionally, like:

def internal_error(exc: peewee.InternalError) -> Optional[Response]:
    if exc.args[0] == 1213:
        # Deadlock found when trying to get lock; try restarting transaction
        return CustomErrorResponse(...)

def any_exception(exc: Exception) -> Response:
        return CustomErrorResponse(...)

app.register_error_handler(peewee.InternalError, internal_error)
app.register_error_handler(Exception, any_exception)

Could it be so that if None is returned from an errorhandler, Flask keeps on iterating and calls the next suitable errorhandler?

Lines: https://github.com/pallets/flask/blob/master/flask/app.py#L1521-L1525 Looks like the code has changed somewhat since 0.10.1 that I'm running (in 0.10.1 this would have been 3-line change I think).

Of course I could call return any_exception(exc) from internal_error() but that kinda adds direct dependency between the two methods and I would like to avoid it (consider the error handlers coming from different modules/classes etc. and maybe there's even third error handler sitting between that would be skipped here).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions