♻️ Default to JSON api errors for blueprints as well

Behaviour can be overwritten via BluePrintPlugin
.get_blueprint_api_prefixes.
This commit is contained in:
Gina Häußge committed 2021-03-10 21:54:36 +01:00
1 parent b1596f30d9
commit cf2ce1e88b
2 files changed
+25 -3

No files matched your search

+14 -1
View File
@@ -1477,9 +1477,22 @@ class BlueprintPlugin(OctoPrintPlugin, RestartNeedingPlugin):
If you want your blueprint's endpoints to have specific permissions, return ``False`` for this and do your
permissions checks explicitly.
"""
return True
# noinspection PyMethodMayBeStatic
def get_blueprint_api_prefixes(self):
"""
Return all prefixes of your endpoint that are an API that should be containing JSON only.
Anything that matches this will generate JSON error messages in case of flask.abort
calls, instead of the default HTML ones.
Defaults to all endpoints under the blueprint. Limit this further as needed. E.g.,
if you only want your endpoints /foo, /foo/1 and /bar to be declared as API,
return ``["/foo", "/bar"]``. A match will be determined via startswith.
"""
return [""]
class SettingsPlugin(OctoPrintPlugin):
"""
+11 -2
View File
@@ -1741,7 +1741,12 @@ class Server(object):
json_errors = ["/api"]
# also register any blueprints defined in BlueprintPlugins
blueprints.update(self._prepare_blueprint_plugins())
(
blueprint_from_plugins,
api_prefixes_from_plugins,
) = self._prepare_blueprint_plugins()
blueprints.update(blueprint_from_plugins)
json_errors += api_prefixes_from_plugins
# and register a blueprint for serving the static files of asset plugins which are not blueprint plugins themselves
blueprints.update(self._prepare_asset_plugins())
@@ -1762,6 +1767,7 @@ class Server(object):
def _prepare_blueprint_plugins(self):
blueprints = OrderedDict()
api_prefixes = []
blueprint_plugins = octoprint.plugin.plugin_manager().get_implementations(
octoprint.plugin.BlueprintPlugin
@@ -1769,6 +1775,9 @@ class Server(object):
for plugin in blueprint_plugins:
try:
blueprint, prefix = self._prepare_blueprint_plugin(plugin)
api_prefixes += map(
lambda x: prefix + x, plugin.get_blueprint_api_prefixes()
)
blueprints[prefix] = blueprint
except Exception:
self._logger.exception(
@@ -1778,7 +1787,7 @@ class Server(object):
)
continue
return blueprints
return blueprints, api_prefixes
def _prepare_asset_plugins(self):
blueprints = OrderedDict()