Add a BuildError hook to url_for, #456.
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Ron DuPlain · committed by Ron DuPlain · parent d90f0af · Browse files · 3 files·+46 added−0 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+46 added−0 removed
- other:+46 added−0 removed
Provenance
- Commit
bb31188- Tree
d15d7d2- Author
Ron DuPlain <ron.duplain@gmail.com>- Committer
Ron DuPlain <ron.duplain@gmail.com>- Parent
d90f0af
3 files changed+46 added−0 removed
modified:flask/testsuite/basic.py+12 added
17 unmodified lines18 from threading import Thread19 from flask.testsuite import FlaskTestCase, emits_module_deprecation_warning20 from werkzeug.exceptions import BadRequest, NotFound21 from werkzeug.http import parse_date22+from werkzeug.routing import BuildError23 24 25 class BasicFunctionalityTestCase(FlaskTestCase):26 668 unmodified lines695 self.assert_equal(flask.url_for('hello', name='test x'), '/hello/test%20x')696 self.assert_equal(flask.url_for('hello', name='test x', _external=True),697 'http://localhost/hello/test%20x')698 699+ def test_build_error_handler(self):700+ app = flask.Flask(__name__)701+ with app.test_request_context():702+ self.assertRaises(BuildError, flask.url_for, 'spam')703+ def handler(error, endpoint, **values):704+ # Just a test.705+ return '/test_handler/'706+ app.build_error_handler = handler707+ with app.test_request_context():708+ self.assert_equal(flask.url_for('spam'), '/test_handler/')709+710 def test_custom_converters(self):711 from werkzeug.routing import BaseConverter712 class ListConverter(BaseConverter):713 def to_python(self, value):445 unmodified linesmodified:flask/app.py+20 added
327 unmodified lines328 #: To register a error handler, use the :meth:`errorhandler`329 #: decorator.330 self.error_handler_spec = {None: self._error_handlers}331 332+ #: If not `None`, this function is called when :meth:`url_for` raises333+ #: :exc:`~werkzeug.routing.BuildError`, with the call signature::334+ #:335+ #: self.build_error_handler(error, endpoint, **values)336+ #:337+ #: Here, `error` is the instance of `BuildError`, and `endpoint` and338+ #: `**values` are the arguments passed into :meth:`url_for`.339+ #:340+ #: .. versionadded:: 0.9341+ self.build_error_handler = None342+343 #: A dictionary with lists of functions that should be called at the344 #: beginning of the request. The key of the dictionary is the name of345 #: the blueprint this function is active for, `None` for all requests.346 #: This can for example be used to open database connections or1136 unmodified lines1483 funcs = chain(funcs, self.url_default_functions.get(bp, ()))1484 for func in funcs:1485 func(endpoint, values)1486 1487+ def handle_build_error(self, error, endpoint, **values):1488+ """Handle :class:`~werkzeug.routing.BuildError` on :meth:`url_for`.1489+1490+ Calls :attr:`build_error_handler` if it is not `None`.1491+ """1492+ if self.build_error_handler is None:1493+ raise error1494+ return self.build_error_handler(error, endpoint, **values)1495+1496 def preprocess_request(self):1497 """Called before the actual request dispatching and will1498 call every as :meth:`before_request` decorated function.1499 If any of these function returns a value it's handled as181 unmodified linesmodified:flask/helpers.py+14 added
18 unmodified lines19 import mimetypes20 from time import time21 from zlib import adler3222 from threading import RLock23+from werkzeug.routing import BuildError24 from werkzeug.urls import url_quote25 26 # try to load the best simplejson implementation available. If JSON27 # is not installed, we add a failing class.186 unmodified lines214 215 .. versionadded:: 0.9216 The `_anchor` and `_method` parameters were added.217 218+ .. versionadded:: 0.9219+ Calls :meth:`Flask.handle_build_error` on220+ :exc:`~werkzeug.routing.BuildError`.221+222 :param endpoint: the endpoint of the URL (name of the function)223 :param values: the variable arguments of the URL rule224 :param _external: if set to `True`, an absolute URL is generated.225 :param _anchor: if provided this is added as anchor to the URL.38 unmodified lines264 265 anchor = values.pop('_anchor', None)266 method = values.pop('_method', None)267 appctx.app.inject_url_defaults(endpoint, values)268+ try:269+ rv = url_adapter.build(endpoint, values, method=method,270+ force_external=external)271+ except BuildError, error:272+ values['_external'] = external273+ values['_anchor'] = anchor274+ values['_method'] = method275+ return appctx.app.handle_build_error(error, endpoint, **values)276+277 rv = url_adapter.build(endpoint, values, method=method,278 force_external=external)279 if anchor is not None:280 rv += '#' + url_quote(anchor)502 unmodified lines