Module: check_mk
Branch: master
Commit: 0529cd3aa582ea6a4bf18c1bad094b4f01d8e6fa
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=0529cd3aa582ea…
Author: Lars Michelsen <lm(a)mathias-kettner.de>
Date: Wed Jan 20 12:09:42 2016 +0100
#2904 FIX Improved error handling when custom url dashlets raise an exception
Fixed whole dashboards crashes when a "Custom URL dashlet" using a "Custom
URL
rendering function" raise an exception while calling that function.
---
.werks/2904 | 10 +++++++
ChangeLog | 1 +
web/htdocs/dashboard.py | 53 +++++++++++++++++++++++--------------
web/plugins/dashboard/dashlets.py | 11 +++++++-
4 files changed, 54 insertions(+), 21 deletions(-)
diff --git a/.werks/2904 b/.werks/2904
new file mode 100644
index 0000000..a277077
--- /dev/null
+++ b/.werks/2904
@@ -0,0 +1,10 @@
+Title: Improved error handling when custom url dashlets raise an exception
+Level: 1
+Component: multisite
+Compatible: compat
+Version: 1.2.7i4
+Date: 1453287618
+Class: fix
+
+Fixed whole dashboards crashes when a "Custom URL dashlet" using a "Custom
URL
+rendering function" raise an exception while calling that function.
diff --git a/ChangeLog b/ChangeLog
index 0a036c3..173b09d 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -224,6 +224,7 @@
* 2896 FIX: Improved handling of wrong encoded input strings...
* 2822 FIX: Availabiltiy: Fixed exception while rendering the timeline...
* 2901 FIX: Fixed issue editing availability annotations...
+ * 2904 FIX: Improved error handling when custom url dashlets raise an exception...
WATO:
* 2442 WATO remove host: improved cleanup of obsolete host files...
diff --git a/web/htdocs/dashboard.py b/web/htdocs/dashboard.py
index 0fb4a8d..7d00b29 100644
--- a/web/htdocs/dashboard.py
+++ b/web/htdocs/dashboard.py
@@ -373,26 +373,7 @@ def render_dashboard(name):
# dashlets using the 'urlfunc' method will dynamically compute
# an url (using HTML context variables at their wish).
if "urlfunc" in dashlet:
- urlfunc = dashlet['urlfunc']
- # We need to support function pointers to be compatible to old dashboard
plugin
- # based definitions. The new dashboards use strings to reference functions
within
- # the global context or functions of a module. An example would be:
- #
- # urlfunc: "my_custom_url_rendering_function"
- #
- # or within a module:
- #
- # urlfunc: "my_module.render_my_url"
- if type(urlfunc) == type(lambda x: x):
- dashlet["url"] = urlfunc()
- else:
- if '.' in urlfunc:
- module_name, func_name = urlfunc.split('.', 1)
- module = __import__(module_name)
- fp = module.__dict__[func_name]
- else:
- fp = globals()[urlfunc]
- dashlet["url"] = fp()
+ gather_dashlet_url(dashlet)
dashlet_type = dashlet_types[dashlet['type']]
@@ -572,6 +553,38 @@ def render_dashlet_content(nr, the_dashlet, stash_html_vars=True):
if stash_html_vars:
html.unstash_vars()
+# Use the URL returned by urlfunc as dashlet URL
+#
+# We need to support function pointers to be compatible to old dashboard plugin
+# based definitions. The new dashboards use strings to reference functions within
+# the global context or functions of a module. An example would be:
+#
+# urlfunc: "my_custom_url_rendering_function"
+#
+# or within a module:
+#
+# urlfunc: "my_module.render_my_url"
+def gather_dashlet_url(dashlet):
+ try:
+ urlfunc = dashlet['urlfunc']
+ if type(urlfunc) == type(lambda x: x):
+ dashlet["url"] = urlfunc()
+ else:
+ if '.' in urlfunc:
+ module_name, func_name = urlfunc.split('.', 1)
+ module = __import__(module_name)
+ fp = module.__dict__[func_name]
+ else:
+ fp = globals()[urlfunc]
+ dashlet["url"] = fp()
+ except Exception, e:
+ # When an exception occurs the "url" key will be missing in the
dashlet
+ # and an error is shown in the dashlet while rendering it. This is enough.
+ # For details the user can enable debugging.
+ if config.debug:
+ raise
+
+
# Create the HTML code for one dashlet. Each dashlet has an id "dashlet_%d",
# where %d is its index (in board["dashlets"]). Javascript uses that id
# for the resizing. Within that div there is an inner div containing the
diff --git a/web/plugins/dashboard/dashlets.py b/web/plugins/dashboard/dashlets.py
index 4b62454..a1e6a07 100644
--- a/web/plugins/dashboard/dashlets.py
+++ b/web/plugins/dashboard/dashlets.py
@@ -649,7 +649,16 @@ dashlet_types["view"] = {
def dashlet_url(dashlet):
if dashlet.get('show_in_iframe', True):
- return dashlet['url']
+ try:
+ return dashlet['url']
+ except KeyError:
+ if "urlfunc" in dashlet:
+ raise MKUserError(None,
+ _("You set the dashlet to use a dynamic URL rendering function.
It seems "
+ "that this function call failed. You may want to open the
dashboard "
+ "in debug mode to get more details."))
+ else:
+ raise
def dashlet_url_validate(value, varprefix):
if 'url' not in value and 'urlfunc' not in value: