From 1f31ec4bea20e67f1870a0857c869eafa109c4d4 Mon Sep 17 00:00:00 2001 From: Armin Ronacher Date: Sun, 5 Jun 2011 10:32:00 +0200 Subject: [PATCH] Added documentation in update document for new error handling --- docs/upgrading.rst | 27 +++++++++++++++++++++++++++ flask/app.py | 9 +++++++++ 2 files changed, 36 insertions(+) diff --git a/docs/upgrading.rst b/docs/upgrading.rst index 7d6e0f0a..903524ca 100644 --- a/docs/upgrading.rst +++ b/docs/upgrading.rst @@ -91,6 +91,33 @@ handling:: # and here it's gone +Manual Error Handler Attaching +`````````````````````````````` + +While it is still possible to attach error handlers to +:attr:`Flask.error_handlers` it's discouraged to do so and in fact +deprecated. In generaly we no longer recommend custom error handler +attaching via assignments to the underlying dictionary due to the more +complex internal handling to support arbitrary exception classes and +blueprints. See :meth:`Flask.errorhandler` for more information. + +The proper upgrade is to change this:: + + app.error_handlers[403] = handle_error + +Into this:: + + app.register_error_handler(403, handle_error) + +Alternatively you should just attach the function with a decorator:: + + @app.errorhandler(403) + def handle_error(e): + ... + +(Note that :meth:`register_error_handler` is new in Flask 0.7) + + Version 0.6 ----------- diff --git a/flask/app.py b/flask/app.py index 8f371dba..b76e69e6 100644 --- a/flask/app.py +++ b/flask/app.py @@ -822,6 +822,15 @@ class Flask(_PackageBoundObject): return f return decorator + def register_error_handler(self, code_or_exception, f): + """Alternative error attach function to the :meth:`errorhandler` + decorator that is more straightforward to use for non decorator + usage. + + .. versionadded:: 0.7 + """ + self._register_error_handler(None, code_or_exception, f) + def _register_error_handler(self, key, code_or_exception, f): if isinstance(code_or_exception, HTTPException): code_or_exception = code_or_exception.code