From b6957ca04a5cef5b59a89c7679c518ef8bf1aaae Mon Sep 17 00:00:00 2001 From: Micke Nordin Date: Tue, 25 Oct 2022 09:10:11 +0200 Subject: [PATCH] Better error handling --- requirements.txt | 3 ++- scripts/knotctl | 22 ++++++++++++++-------- stdeb.cfg | 2 +- 3 files changed, 17 insertions(+), 10 deletions(-) diff --git a/requirements.txt b/requirements.txt index ca527d4..a6e68ca 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,4 @@ argcomplete==2.0.0 -requests==2.27.1 pyyaml==5.4.1 +requests==2.27.1 +simplejson==3.17.6 diff --git a/scripts/knotctl b/scripts/knotctl index 3ae2f94..601cfbe 100755 --- a/scripts/knotctl +++ b/scripts/knotctl @@ -13,7 +13,9 @@ from typing import Union import argcomplete import requests import yaml +from requests.exceptions import JSONDecodeError as RequestsJSONDecodeError from requests.models import HTTPBasicAuth +from simplejson.errors import JSONDecodeError as SimplejsonJSONDecodeError # Helper functions @@ -241,14 +243,18 @@ def main() -> int: if 'ttl' in args: ttl = args.ttl url = setup_url(baseurl, args.data, args.name, args.rtype, ttl, args.zone) - if args.command == "add": - run_add(url, args.json, headers) - elif args.command == "delete": - run_delete(url, args.json, headers) - elif args.command == "list": - run_list(url, args.json, headers) - elif args.command == "update": - run_update(url, args.json, headers) + try: + if args.command == "add": + run_add(url, args.json, headers) + elif args.command == "delete": + run_delete(url, args.json, headers) + elif args.command == "list": + run_list(url, args.json, headers) + elif args.command == "update": + run_update(url, args.json, headers) + except (RequestsJSONDecodeError, SimplejsonJSONDecodeError): + output( + error("Could not decode api response as JSON", "Could not decode")) return 0 diff --git a/stdeb.cfg b/stdeb.cfg index cb52e6a..520385f 100644 --- a/stdeb.cfg +++ b/stdeb.cfg @@ -1,4 +1,4 @@ [DEFAULT] -Depends3: python3-argcomplete, python3-requests, python3-yaml +Depends3: python3-argcomplete, python3-requests, python3-simplejson, python3-yaml Debian-Version: 1 Package3: knotctl