Improved interface for the URL build error handler
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 e78e2a1 · Browse files · 3 files·+29 added−25 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+29 added−25 removed
- other:+29 added−25 removed
Provenance
- Commit
2053d04- Tree
92a8be8- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
e78e2a1
3 files changed+29 added−25 removed
modified:flask/testsuite/basic.py+3 added-3 removed
710 unmodified lines711 pass712 try:713 raise RuntimeError('Test case where BuildError is not current.')714 except RuntimeError:715- self.assertRaises(BuildError, app.handle_build_error, error, 'spam')715+ self.assertRaises(BuildError, app.handle_url_build_error, error, 'spam', {})716 717 # Test a custom handler.718- def handler(error, endpoint, **values):718+ def handler(error, endpoint, values):719 # Just a test.720 return '/test_handler/'721- app.build_error_handler = handler721+ app.url_build_error_handlers.append(handler)722 with app.test_request_context():723 self.assert_equal(flask.url_for('spam'), '/test_handler/')724 725 def test_custom_converters(self):448 unmodified linesmodified:flask/app.py+23 added-20 removed
17 unmodified lines18 from itertools import chain19 from functools import update_wrapper20 21 from werkzeug.datastructures import ImmutableDict22-from werkzeug.routing import Map, Rule, RequestRedirect22+from werkzeug.routing import Map, Rule, RequestRedirect, BuildError23 from werkzeug.exceptions import HTTPException, InternalServerError, \24 MethodNotAllowed, BadRequest25 26 from .helpers import _PackageBoundObject, url_for, get_flashed_messages, \313 unmodified lines340 #: To register a error handler, use the :meth:`errorhandler`341 #: decorator.342 self.error_handler_spec = {None: self._error_handlers}343 344- #: If not `None`, this function is called when :meth:`url_for` raises345- #: :exc:`~werkzeug.routing.BuildError`, with the call signature::344+ #: A list of functions that are called when :meth:`url_for` raises a345+ #: :exc:`~werkzeug.routing.BuildError`. Each function registered here346+ #: is called with `error`, `endpoint` and `values`. If a function347+ #: returns `None` or raises a `BuildError` the next function is348+ #: tried.349 #:347- #: self.build_error_handler(error, endpoint, **values)348- #:349- #: Here, `error` is the instance of `BuildError`, and `endpoint` and350- #: `**values` are the arguments passed into :meth:`url_for`.351- #:350 #: .. versionadded:: 0.9353- self.build_error_handler = None351+ self.url_build_error_handlers = []352 353 #: A dictionary with lists of functions that should be called at the354 #: beginning of the request. The key of the dictionary is the name of355 #: the blueprint this function is active for, `None` for all requests.1131 unmodified lines1487 funcs = chain(funcs, self.url_default_functions.get(bp, ()))1488 for func in funcs:1489 func(endpoint, values)1490 1493- def handle_build_error(self, error, endpoint, **values):1491+ def handle_url_build_error(self, error, endpoint, values):1492 """Handle :class:`~werkzeug.routing.BuildError` on :meth:`url_for`.1495-1496- Calls :attr:`build_error_handler` if it is not `None`.1493 """1498- if self.build_error_handler is None:1499- exc_type, exc_value, tb = sys.exc_info()1500- if exc_value is error:1501- # exception is current, raise in context of original traceback.1502- raise exc_type, exc_value, tb1503- else:1504- raise error1505- return self.build_error_handler(error, endpoint, **values)1494+ exc_type, exc_value, tb = sys.exc_info()1495+ for handler in self.url_build_error_handlers:1496+ try:1497+ rv = handler(error, endpoint, values)1498+ if rv is not None:1499+ return rv1500+ except BuildError, error:1501+ pass1502 1503+ # At this point we want to reraise the exception. If the error is1504+ # still the same one we can reraise it with the original traceback,1505+ # otherwise we raise it from here.1506+ if error is exc_value:1507+ raise exc_type, exc_value, tb1508+ raise error1509+1510 def preprocess_request(self):1511 """Called before the actual request dispatching and will1512 call every as :meth:`before_request` decorated function.1513 If any of these function returns a value it's handled as181 unmodified linesmodified:flask/helpers.py+3 added-2 removed
9 unmodified lines10 """11 12 from __future__ import with_statement13 14-import imp14 import os15 import sys16 import pkgutil17 import posixpath283 unmodified lines301 try:302 rv = url_adapter.build(endpoint, values, method=method,303 force_external=external)304 except BuildError, error:305+ # We need to inject the values again so that the app callback can306+ # deal with that sort of stuff.307 values['_external'] = external308 values['_anchor'] = anchor309 values['_method'] = method309- return appctx.app.handle_build_error(error, endpoint, **values)310+ return appctx.app.handle_url_build_error(error, endpoint, values)311 312 rv = url_adapter.build(endpoint, values, method=method,313 force_external=external)314 if anchor is not None:516 unmodified lines