Browse Source

pretty exception implementation

marton levente 11 months ago
parent
commit
1eb3267f83
1 changed files with 15 additions and 3 deletions
  1. 15 3
      anafapi/anafapi.py

+ 15 - 3
anafapi/anafapi.py

@@ -3,6 +3,8 @@ import os
 import platform
 import subprocess as sp
 import time
+import traceback
+import sys
 from json import JSONDecodeError
 from urllib.parse import urljoin
 from collections import OrderedDict
@@ -14,6 +16,12 @@ from .writer import Writer
 def insert_keys(dict_, obj, pos):
     return {k: v for k, v in (list(dict_.items())[:pos] + list(obj.items()) + list(dict_.items())[pos:])}
 
+def pretty_exc():
+    exc_type, _exc_obj, exc_tb = sys.exc_info()
+    # fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
+    for tb in list(traceback.format_exception(exc_type, _exc_obj, exc_tb)):
+        print(tb)
+
 class anafAPI(object):
     '''classdocs
     '''
@@ -55,8 +63,10 @@ class anafAPI(object):
             #     pos += 1
             processed_resp_dict = self.process_resp(resp_dict)
             new_dict = {processed_resp_dict['cui']: self._dict_to_list(processed_resp_dict, 0)}
-        except (KeyError, JSONDecodeError):
-            pass
+        except (KeyError, JSONDecodeError, IndexError):
+            print(result.text)
+            pretty_exc()
+            # pass
         time.sleep(1.1)
         return new_dict, processed_resp_dict
         # return resp_dict['date_generale']
@@ -69,7 +79,9 @@ class anafAPI(object):
             resp_dict = result.json()['found'][0]
             new_dict = {resp_dict['cui']: self._dict_to_list(resp_dict, 0)}
         except (KeyError, JSONDecodeError):
-            pass
+            print(result.text)
+            pretty_exc()
+            # pass
         time.sleep(1.1)
         return new_dict, resp_dict