From 3f30828e1a6fa3c21fb1cdd9968100b1cd107863 Mon Sep 17 00:00:00 2001 From: R4SAS Date: Thu, 27 Apr 2023 21:47:24 +0300 Subject: [PATCH] handle errors when trying to get instance jsonld schema Signed-off-by: R4SAS --- pbincli/api.py | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/pbincli/api.py b/pbincli/api.py index 82f5197..1b3facd 100644 --- a/pbincli/api.py +++ b/pbincli/api.py @@ -81,14 +81,19 @@ class PrivateBin: def getVersion(self): - jsonldSchema = self.session.get( + result = self.session.get( url = self.server + '?jsonld=paste', - headers = self.headers).json() - return jsonldSchema['@context']['v']['@value'] \ - if ('@context' in jsonldSchema and - 'v' in jsonldSchema['@context'] and - '@value' in jsonldSchema['@context']['v']) \ - else 1 + headers = self.headers) + try: + jsonldSchema = result.json() + return jsonldSchema['@context']['v']['@value'] \ + if ('@context' in jsonldSchema and + 'v' in jsonldSchema['@context'] and + '@value' in jsonldSchema['@context']['v']) \ + else 1 + except ValueError: + PBinCLIError("Unable parse response as json. Received (size = {}):\n{}".format(len(result.text), result.text)) + def getServer(self): return self.server