Skip to content
Myr

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

3 files+46 added−0 removed

modified:flask/testsuite/basic.py+12 added
17 unmodified lines
18from threading import Thread19from flask.testsuite import FlaskTestCase, emits_module_deprecation_warning20from werkzeug.exceptions import BadRequest, NotFound21from werkzeug.http import parse_date22from werkzeug.routing import BuildError232425class BasicFunctionalityTestCase(FlaskTestCase):26
668 unmodified lines
695 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')698699 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/')709710 def test_custom_converters(self):711 from werkzeug.routing import BaseConverter712 class ListConverter(BaseConverter):713 def to_python(self, value):
445 unmodified lines
modified:flask/app.py+20 added
327 unmodified lines
328 #: To register a error handler, use the :meth:`errorhandler`329 #: decorator.330 self.error_handler_spec = {None: self._error_handlers}331332 #: 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 = None342343 #: 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 or
1136 unmodified lines
1483 funcs = chain(funcs, self.url_default_functions.get(bp, ()))1484 for func in funcs:1485 func(endpoint, values)14861487 def handle_build_error(self, error, endpoint, **values):1488 """Handle :class:`~werkzeug.routing.BuildError` on :meth:`url_for`.14891490 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)14951496 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 as
181 unmodified lines
modified:flask/helpers.py+14 added
18 unmodified lines
19import mimetypes20from time import time21from zlib import adler3222from threading import RLock23from werkzeug.routing import BuildError24from werkzeug.urls import url_quote2526# try to load the best simplejson implementation available. If JSON27# is not installed, we add a failing class.
186 unmodified lines
214215 .. versionadded:: 0.9216 The `_anchor` and `_method` parameters were added.217218 .. versionadded:: 0.9219 Calls :meth:`Flask.handle_build_error` on220 :exc:`~werkzeug.routing.BuildError`.221222 :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 lines
264265 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)276277 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