From 3ead80a56ff181d0f74087ea7f543f316a96018a Mon Sep 17 00:00:00 2001 From: Steven Loria Date: Sun, 15 Jul 2018 11:29:25 -0400 Subject: [PATCH] Pass schema instance to error handlers * @error_handler and handle_error receive schema instance * In Flask, the Schema instance is attached to HTTPExceptions and is accessible from error.data["schema"] See https://github.com/marshmallow-code/marshmallow/issues/840#issuecomment-403481686 --- CHANGELOG.rst | 24 ++++++++++++++++++++++++ docs/framework_support.rst | 3 ++- docs/quickstart.rst | 8 ++++---- tests/apps/flask_app.py | 1 + tests/test_core.py | 8 +++++--- webargs/aiohttpparser.py | 2 +- webargs/asyncparser.py | 2 +- webargs/bottleparser.py | 2 +- webargs/core.py | 26 +++++++++++++++----------- webargs/falconparser.py | 2 +- webargs/flaskparser.py | 4 ++-- webargs/pyramidparser.py | 2 +- webargs/tornadoparser.py | 2 +- 13 files changed, 59 insertions(+), 27 deletions(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 42944556..355007c5 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -4,6 +4,30 @@ Changelog 4.0.0 (unreleased) ****************** +Features: + +* *Backwards-incompatible*: Custom error handlers receive the + `marshmallow.Schema` instance as the third argument. Update any + functions decorated with `Parser.error_handler` to take a ``schema`` + argument, like so: + +.. code-block:: python + + # 3.x + @parser.error_handler + def handle_error(error, req): + raise CustomError(error.messages) + + + # 4.x + @parser.error_handler + def handle_error(error, req, schema): + raise CustomError(error.messages) + + +See `marshmallow-code/marshmallow#840 (comment) `_ +for more information about this change. + Bug fixes: * *Backwards-incompatible*: Rename ``webargs.async`` to diff --git a/docs/framework_support.rst b/docs/framework_support.rst index a81754dc..a81cd578 100644 --- a/docs/framework_support.rst +++ b/docs/framework_support.rst @@ -31,7 +31,8 @@ When using the :meth:`use_args ` decor Error Handling ++++++++++++++ -Webargs uses Flask's ``abort`` function to raise an ``HTTPException`` when a validation error occurs. If you use the ``Flask.errorhandler`` method to handle errors, you can access validation messages from the ``data`` attribute of an error. +Webargs uses Flask's ``abort`` function to raise an ``HTTPException`` when a validation error occurs. +If you use the ``Flask.errorhandler`` method to handle errors, you can access validation messages from the ``data`` attribute of an error. Here is an example error handler that returns validation messages to the client as JSON. diff --git a/docs/quickstart.rst b/docs/quickstart.rst index bf7cda98..aeb5b330 100644 --- a/docs/quickstart.rst +++ b/docs/quickstart.rst @@ -185,14 +185,14 @@ Error Handling -------------- Each parser has a default error handling method. To override the error handling callback, write a function that -receives an error and the request and handles the error. +receives an error, the request, and the `marshmallow.Schema` instance. Then decorate that function with :func:`Parser.error_handler `. .. code-block:: python - from webargs import core + from webargs import flaskparser - parser = core.Parser() + parser = flaskparser.FlaskParser() class CustomError(Exception): @@ -200,7 +200,7 @@ Then decorate that function with :func:`Parser.error_handler