Added a PROPAGATE_EXCEPTIONS flag
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Armin Ronacher · committed by Armin Ronacher · parent ed517c7 · Browse files · 4 files·+54 added−2 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+54 added−2 removed
- tests:+28 added−1 removed
- other:+26 added−1 removed
Provenance
- Commit
8569dfe- Tree
30acd80- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
ed517c7
4 files changed+54 added−2 removed
modified:docs/config.rst+8 added
52 unmodified lines53 54 =============================== =========================================55 ``DEBUG`` enable/disable debug mode56 ``TESTING`` enable/disable testing mode57+``PROPAGATE_EXCEPTIONS`` explicitly enable or disable the58+ propagation of exceptions. If not set or59+ explicitly set to `None` this is60+ implicitly true if either `TESTING` or61+ `DEBUG` is true.62 ``SECRET_KEY`` the secret key63 ``SESSION_COOKIE_NAME`` the name of the session cookie64 ``PERMANENT_SESSION_LIFETIME`` the lifetime of a permanent session as65 :class:`datetime.timedelta` object.34 unmodified lines100 101 .. versionadded:: 0.6102 ``MAX_CONTENT_LENGTH``103 104+.. versionadded:: 0.7105+ ``PROPAGATE_EXCEPTIONS``106+107 Configuring from Files108 ----------------------109 110 Configuration becomes more useful if you can configure from a file, and126 unmodified linesmodified:flask/app.py+14 added-1 removed
187 unmodified lines188 #: Default configuration parameters.189 default_config = ImmutableDict({190 'DEBUG': False,191 'TESTING': False,192+ 'PROPAGATE_EXCEPTIONS': None,193 'SECRET_KEY': None,194 'SESSION_COOKIE_NAME': 'session',195 'PERMANENT_SESSION_LIFETIME': timedelta(days=31),196 'USE_X_SENDFILE': False,107 unmodified lines304 self.jinja_env = self.create_jinja_environment()305 self.init_jinja_globals()306 307 @property308+ def propagate_exceptions(self):309+ """Returns the value of the `PROPAGATE_EXCEPTIONS` configuration310+ value in case it's set, otherwise a sensible default is returned.311+312+ .. versionadded:: 0.7313+ """314+ rv = self.config['PROPAGATE_EXCEPTIONS']315+ if rv is not None:316+ return rv317+ return self.testing or self.debug318+319+ @property320 def logger(self):321 """A :class:`logging.Logger` object for this application. The322 default configuration is to log to stderr if the application is323 in debug mode. This logger can be used to (surprise) log messages.370 unmodified lines694 .. versionadded: 0.3695 """696 got_request_exception.send(self, exception=e)697 handler = self.error_handlers.get(500)685- if self.debug:698+ if self.propagate_exceptions:699 raise700 self.logger.exception('Exception on %s [%s]' % (701 request.path,702 request.method233 unmodified linesmodified:tests/flask_tests.py+28 added-1 removed
13 unmodified lines14 import re15 import sys16 import flask17 import unittest18+from threading import Thread19 from logging import StreamHandler20 from contextlib import contextmanager21 from datetime import datetime22 from werkzeug import parse_date, parse_options_header524 unmodified lines547 raise ValueError(548 "No ValueError exception should have been raised \"%s\"" % e549 )550 550-551 def test_test_app_proper_environ(self):552 app = flask.Flask(__name__)553 app.config.update(554 SERVER_NAME='localhost.localdomain:5000'63 unmodified lines618 raise ValueError(619 "No ValueError exception should have been raised \"%s\"" % e620 )621 622+ def test_exception_propagation(self):623+ def apprunner(configkey):624+ app = flask.Flask(__name__)625+ @app.route('/')626+ def index():627+ 1/0628+ c = app.test_client()629+ if config_key is not None:630+ app.config[config_key] = True631+ try:632+ resp = c.get('/')633+ except Exception:634+ pass635+ else:636+ self.fail('expected exception')637+ else:638+ assert c.get('/').status_code == 500639 640+ # we have to run this test in an isolated thread because if the641+ # debug flag is set to true and an exception happens the context is642+ # not torn down. This causes other tests that run after this fail643+ # when they expect no exception on the stack.644+ for config_key in 'TESTING', 'PROPAGATE_EXCEPTIONS', 'DEBUG', None:645+ t = Thread(target=apprunner, args=(config_key,))646+ t.start()647+ t.join()648+649+650 class JSONTestCase(unittest.TestCase):651 652 def test_jsonify(self):653 d = dict(a=23, b=42, c=[1, 2, 3])744 unmodified linesmodified:CHANGES+4 added
21 unmodified lines22 was not reliable and caused issues on Google's App Engine. Until23 1.0 the old behaviour will continue to work but issue dependency24 warnings.25 - fixed a problem for Flask to run on jython.26+- added a `PROPAGATE_EXCEPTIONS` configuration variable that can be27+ used to flip the setting of exception propagation which previously28+ was linked to `DEBUG` alone and is now linked to either `DEBUG` or29+ `TESTING`.30 31 Version 0.6.132 -------------33 156 unmodified lines