Webargs

API

class twopi_flask_utils.webargs.BetterFlaskParser(locations=None, error_handler=None, schema_class=None)

A Flask-Restful compatible parser for WebArgs.

handle_error(error)

Don’t raise a HTTPException via abort. Instead we will throw the ValidationError and handle it with our flask Exception handler.

This allows a common code path for both Flask-Restful AND standard Flask Views.

twopi_flask_utils.webargs.handle_validation_error(exc)

When using BetterFlaskParser, if an exception occurs, it will throw the original ValidationError. This circumvents the capture inside Flask-Restful (if it is being used at all).

Instead of capturing all 422 HTTPExceptions, you register this error handler with ValidationError:

app.errorhandler(ValidationError)(handle_validation_error)

This function will produce a jsonified response with the field errors from the ValidationError.

Warning

This handler is incompatible with the standard FlaskParser, since it throws HTTPExceptions (via abort). Registering this handler with errorhandler(422) will not work.