ysde / grafana-backup-tool

A Python-based application to backup Grafana settings by using the Grafana API
MIT License
860 stars 273 forks source link

Handle RequestsJSONDecodeError exceptions #256

Open nemobis opened 8 months ago

nemobis commented 8 months ago

I'm using the latest grafana-backup with Grafana v10.0.2. A vanilla grafana-backup save downloads a few dashboards but then crashes on one of them (I think the URL might have been redirected somewhere else). It would be nice to just skip upon such errors.


Traceback (most recent call last):
  File "/home/federico/.local/bin/grafana-backup", line 8, in <module>
    sys.exit(main())
             ^^^^^^
  File "/home/federico/.local/lib/python3.12/site-packages/grafana_backup/cli.py", line 52, in main
    save(args, settings)
  File "/home/federico/.local/lib/python3.12/site-packages/grafana_backup/save.py", line 70, in main
    backup_functions[backup_function](args, settings)
  File "/home/federico/.local/lib/python3.12/site-packages/grafana_backup/save_dashboards.py", line 27, in main
    save_dashboards_above_Ver6_2(folder_path, log_file, grafana_url, http_get_headers, verify_ssl, client_cert, debug, pretty_print, uid_support, uid_dashboard_slug_suffix)
  File "/home/federico/.local/lib/python3.12/site-packages/grafana_backup/save_dashboards.py", line 100, in save_dashboards_above_Ver6_2
    get_individual_dashboard_setting_and_save(dashboards, folder_path, log_file, grafana_url, http_get_headers, verify_ssl, client_cert, debug, pretty_print, uid_support, slug_suffix)
  File "/home/federico/.local/lib/python3.12/site-packages/grafana_backup/save_dashboards.py", line 65, in get_individual_dashboard_setting_and_save
    (status, content) = get_dashboard(board_uri, grafana_url, http_get_headers, verify_ssl, client_cert, debug)
                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/federico/.local/lib/python3.12/site-packages/grafana_backup/dashboardApi.py", line 125, in get_dashboard
    (status_code, content) = send_grafana_get(
                             ^^^^^^^^^^^^^^^^^
  File "/home/federico/.local/lib/python3.12/site-packages/grafana_backup/dashboardApi.py", line 515, in send_grafana_get
    return (r.status_code, r.json())
                           ^^^^^^^^
  File "/usr/lib/python3.12/site-packages/requests/models.py", line 975, in json
    raise RequestsJSONDecodeError(e.msg, e.doc, e.pos)
requests.exceptions.JSONDecodeError: Expecting value: line 1 column 1 (char 0)