add errorhandler decorator to blueprintplugin

This will allow you to add an errorhandler for your blueprint easily.
This commit is contained in:
Marcel Hellwig 2015-09-11 11:20:41 +02:00
parent 2efc5c4fdb
commit b6053c14f9

View file

@ -1055,6 +1055,24 @@ class BlueprintPlugin(OctoPrintPlugin, RestartNeedingPlugin):
return f return f
return decorator return decorator
@staticmethod
def errorhandler(code_or_exception):
"""
A decorator to mark errorhandlings methods in your BlueprintPlugin subclass. Works just the same as Flask's
own ``errorhandler`` decorator available on blueprints.
See `the documentation for flask.Blueprint.errorhandler <http://flask.pocoo.org/docs/0.10/api/#flask.Blueprint.errorhandler>`_
and `the documentation for flask.Flask.errorhandler <http://flask.pocoo.org/docs/0.10/api/#flask.Flask.errorhandler>`_ for more
information.
"""
from collections import defaultdict
def decorator(f):
if not hasattr(f, "_blueprint_error_handler") or f._blueprint_error_handler is None:
f._blueprint_error_handler = defaultdict(list)
f._blueprint_error_handler[f.__name__].append(code_or_exception)
return f
return decorator
def get_blueprint(self): def get_blueprint(self):
""" """
Creates and returns the blueprint for your plugin. Override this if you want to define and handle your blueprint yourself. Creates and returns the blueprint for your plugin. Override this if you want to define and handle your blueprint yourself.
@ -1073,6 +1091,9 @@ class BlueprintPlugin(OctoPrintPlugin, RestartNeedingPlugin):
for blueprint_rule in f._blueprint_rules[member]: for blueprint_rule in f._blueprint_rules[member]:
rule, options = blueprint_rule rule, options = blueprint_rule
blueprint.add_url_rule(rule, options.pop("endpoint", f.__name__), view_func=f, **options) blueprint.add_url_rule(rule, options.pop("endpoint", f.__name__), view_func=f, **options)
if hasattr(f, "_blueprint_error_handler") and member in f._blueprint_error_handler:
for code_or_exception in f._blueprint_error_handler[member]:
blueprint.errorhandler(code_or_exception)(f)
return blueprint return blueprint
def get_blueprint_kwargs(self): def get_blueprint_kwargs(self):