Webargs¶
API¶
-
class
flask_utils.webargs.BetterFlaskParser(location: Optional[str] = None, *, unknown: Optional[str] = '_default', error_handler: Optional[Callable[[...], NoReturn]] = None, schema_class: Optional[Type[CT_co]] = None)¶ A Flask-Restful compatible parser for WebArgs.
-
handle_error(error)¶ Don’t raise a
HTTPExceptionviaabort. Instead we will throw theValidationErrorand handle it with our flask Exception handler.This allows a common code path for both Flask-Restful AND standard Flask Views.
-
-
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.