More flexible errorhandler #2 #541
Closed
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
DO NOT MERGE !
This PR builds on top of #401 adding ability to specify exception by its HTTP code. Although the code is fine and tests are passing, I would like an opinion on it first.
You can have your own BadRequest handler (see the test).
'data' as dictionary
handle_error
code assumes thatdata
is a dictionary (data.update(custom_data)
). This may not always be the case. For example, if you would like to have a custom error handler on non-HTTPException with.data
attribute as string. This is a problem on existing code already.Adding this PR would make situation much worse. User may return anything from his error handler. Current code will fall through to a series of if-code==XXX-do-this blocks which assume returned data is a dict. In other words, this PR works, but only in happy-path when User is cooperating..
exceptions in 'handle_error'
Exception in custom error handler will be passed down to Flask, which will fall over with..
There is NO indication what (or where) was the original exception.
We could (and should) wrap call to custom error handler with try/catch-all. Thou, the problem may not in inside the handler. This code..
..will also generate the same
AssertionError
. The underlying problem is that default 404 handling assumes that data is a dict.fall-through-ness
Processing won't stop when error handler is found. This is a source of the problems mentioned above. However, somehow the User may depend on this feature. Eg. someone may want add authorization challenge on 401, even if it was already custom handled. I, personally, don't like current fall-trough. Refactoring the code would be quite a major change..
default handlers >=500, 404, 401, 406
I concur the idea.
However, it's not that simple (I tried 😒):
After rewrite, these handlers would have no context other than the exception itself.
self.make_response
Summary
Either this PR is merged as it is.. making small incremental steps forward.
or
Much bigger rewrite is needed. It should be clarified first:
handle_error
code should be rewritten as several smaller error handlersabort(404)
)