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
viaabort
. Instead we will throw theValidationError
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 originalValidationError
. 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 withValidationError
: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 throwsHTTPExceptions
(viaabort
). Registering this handler witherrorhandler(422)
will not work.