Webargs¶
API¶
-
class
flask_utils.webargs.BetterFlaskParser(location: str | None = None, *, unknown: str | None = '_default', error_handler: ErrorHandler | None = None, schema_class: type[ma.Schema] | None = 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.