-
-
Notifications
You must be signed in to change notification settings - Fork 16.3k
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
Fixed and intuitivized exception handling #1291
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
eae48d9
Fixed and intuitivized exception handling
flying-sheep 7126a22
Switched to userdict for obsolete Python versions
flying-sheep a6c6cc1
Removed unused http code get function
flying-sheep aa4700c
More verbose message for old broken behavior
flying-sheep 0e44cca
Removed 500 not being registerable for blueprints
flying-sheep fd8e6b2
removed ExceptionHandlerDict
flying-sheep 8c054f0
added caching
flying-sheep b31252d
addressed a few review concerns
flying-sheep File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
# -*- coding: utf-8 -*- | ||
from werkzeug.exceptions import Forbidden, InternalServerError | ||
import flask | ||
|
||
|
||
def test_error_handler_subclass(): | ||
app = flask.Flask(__name__) | ||
|
||
class ParentException(Exception): | ||
pass | ||
|
||
class ChildExceptionUnregistered(ParentException): | ||
pass | ||
|
||
class ChildExceptionRegistered(ParentException): | ||
pass | ||
|
||
@app.errorhandler(ParentException) | ||
def parent_exception_handler(e): | ||
assert isinstance(e, ParentException) | ||
return 'parent' | ||
|
||
@app.errorhandler(ChildExceptionRegistered) | ||
def child_exception_handler(e): | ||
assert isinstance(e, ChildExceptionRegistered) | ||
return 'child-registered' | ||
|
||
@app.route('/parent') | ||
def parent_test(): | ||
raise ParentException() | ||
|
||
@app.route('/child-unregistered') | ||
def unregistered_test(): | ||
raise ChildExceptionUnregistered() | ||
|
||
@app.route('/child-registered') | ||
def registered_test(): | ||
raise ChildExceptionRegistered() | ||
|
||
|
||
c = app.test_client() | ||
|
||
assert c.get('/parent').data == b'parent' | ||
assert c.get('/child-unregistered').data == b'parent' | ||
assert c.get('/child-registered').data == b'child-registered' | ||
|
||
|
||
def test_error_handler_http_subclass(): | ||
app = flask.Flask(__name__) | ||
|
||
class ForbiddenSubclassRegistered(Forbidden): | ||
pass | ||
|
||
class ForbiddenSubclassUnregistered(Forbidden): | ||
pass | ||
|
||
@app.errorhandler(403) | ||
def code_exception_handler(e): | ||
assert isinstance(e, Forbidden) | ||
return 'forbidden' | ||
|
||
@app.errorhandler(ForbiddenSubclassRegistered) | ||
def subclass_exception_handler(e): | ||
assert isinstance(e, ForbiddenSubclassRegistered) | ||
return 'forbidden-registered' | ||
|
||
@app.route('/forbidden') | ||
def forbidden_test(): | ||
raise Forbidden() | ||
|
||
@app.route('/forbidden-registered') | ||
def registered_test(): | ||
raise ForbiddenSubclassRegistered() | ||
|
||
@app.route('/forbidden-unregistered') | ||
def unregistered_test(): | ||
raise ForbiddenSubclassUnregistered() | ||
|
||
|
||
c = app.test_client() | ||
|
||
assert c.get('/forbidden').data == b'forbidden' | ||
assert c.get('/forbidden-unregistered').data == b'forbidden' | ||
assert c.get('/forbidden-registered').data == b'forbidden-registered' | ||
|
||
|
||
def test_error_handler_blueprint(): | ||
bp = flask.Blueprint('bp', __name__) | ||
|
||
@bp.errorhandler(500) | ||
def bp_exception_handler(e): | ||
return 'bp-error' | ||
|
||
@bp.route('/error') | ||
def bp_test(): | ||
raise InternalServerError() | ||
|
||
app = flask.Flask(__name__) | ||
|
||
@app.errorhandler(500) | ||
def app_exception_handler(e): | ||
return 'app-error' | ||
|
||
@app.route('/error') | ||
def app_test(): | ||
raise InternalServerError() | ||
|
||
app.register_blueprint(bp, url_prefix='/bp') | ||
|
||
c = app.test_client() | ||
|
||
assert c.get('/error').data == b'app-error' | ||
assert c.get('/bp/error').data == b'bp-error' |
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.
This comment was marked as off-topic.
Sorry, something went wrong.
This comment was marked as off-topic.
Sorry, something went wrong.
This comment was marked as off-topic.
Sorry, something went wrong.