Merge ~brad-marshall/charm-grafana/+git/grafana-charm-dashboard-action:action-fix-detail into ~prometheus-charmers/charm-grafana:master

Proposed by Brad Marshall
Status: Merged
Merged at revision: 92a68424cb2af5e38bb3add8faa6b2df37cd2b18
Proposed branch: ~brad-marshall/charm-grafana/+git/grafana-charm-dashboard-action:action-fix-detail
Merge into: ~prometheus-charmers/charm-grafana:master
Diff against target: 40 lines (+10/-3)
1 file modified
actions/import-dashboard (+10/-3)
Reviewer Review Type Date Requested Status
Stuart Bishop (community) Approve
Review via email: mp+327194@code.launchpad.net

Description of the change

Add extra detail to final json decode stanza, fix it to use except rather than else.

To post a comment you must log in.
Revision history for this message
Stuart Bishop (stub) wrote :

Almost every use of a bare except: is a bug in modern Python, and you should use except Exception: here.

I tend to do the following, preserving traceback and ensuring it ends up in the logs via print. It makes diagnosing failures easier:

    except Exception:
        hookenv.action_fail('Unhandled exception')
        tb = traceback.format_exc()
        hookenv.action_set(dict(traceback=tb))
        hookenv.log('Unhandled exception in action {}'.format(action))
        print(tb)

Revision history for this message
Stuart Bishop (stub) wrote :

Approve since this is less busted than before :)

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/actions/import-dashboard b/actions/import-dashboard
2index d61ff5f..2f0854f 100755
3--- a/actions/import-dashboard
4+++ b/actions/import-dashboard
5@@ -4,15 +4,19 @@
6 import requests
7 import json
8 import base64
9+import traceback
10 from charmhelpers.core.hookenv import (
11 action_fail,
12 action_set,
13 action_get,
14 config,
15+ log,
16 )
17
18 from charmhelpers.core import unitdata
19
20+action = "import-dashboard"
21+
22 kv = unitdata.kv()
23 if kv.get('grafana.admin_password'):
24 # print('Admin password: {}'.format(kv.get('grafana.admin_password')))
25@@ -35,9 +39,12 @@ except base64.binascii.Error:
26 except json.JSONDecodeError as e:
27 action_fail("Fail to json decode the dashboard: %s" % (e.msg))
28 exit(0)
29-else:
30- action_fail("Unknown error dealing with dashboard!")
31- exit(0)
32+except Exception:
33+ action_fail('Unhandled exception')
34+ tb = traceback.format_exc()
35+ action_set(dict(traceback=tb))
36+ log('Unhandled exception in action {}'.format(action))
37+ print(tb)
38
39 # Needs to be the format:
40 # { "dashboard": { dashboard },

Subscribers

People subscribed via source and target branches