More error handling
authorNeil Smith <neil.git@njae.me.uk>
Sun, 29 Jan 2017 13:56:50 +0000 (13:56 +0000)
committerNeil Smith <neil.git@njae.me.uk>
Sun, 29 Jan 2017 13:56:50 +0000 (13:56 +0000)
dmarc_to_database

index 38ec02837ae4c6d4caf8ffb080c755ebd5c4a4dd..fb5d458341b55f8204cbadc6f5211a7fb126c416 100755 (executable)
@@ -151,24 +151,27 @@ def build_insert_command(table_name, report, preamble_values=None, i=None):
 
 
 def write_report(connection, cursor, report):
-    insert_string, values = build_insert_command('reports', report)
-    # print(insert_string, values)
-    cursor.execute(insert_string, values)
-    
-    for i in range(1, len(report.findall('./record'))+1):
-        field_names = []
-        cursor.execute('select id, report_metadata_report_id from reports where report_metadata_report_id = %s;', 
-            [report.find('./report_metadata/report_id').text])
-        results = cursor.fetchall()
-        if len(results) != 1:
-            raise RuntimeError('Could not find report record for report item')
-        else:
-            report_id = results[0][0]
-        insert_string, values = build_insert_command('report_items', report, i=i,
-                                                     preamble_values={'report_id': report_id})
+    try:
+        insert_string, values = build_insert_command('reports', report)
         # print(insert_string, values)
         cursor.execute(insert_string, values)
-    connection.commit()
+        
+        for i in range(1, len(report.findall('./record'))+1):
+            field_names = []
+            cursor.execute('select id, report_metadata_report_id from reports where report_metadata_report_id = %s;', 
+                [report.find('./report_metadata/report_id').text])
+            results = cursor.fetchall()
+            if len(results) != 1:
+                raise RuntimeError('Could not find report record for report item')
+            else:
+                report_id = results[0][0]
+            insert_string, values = build_insert_command('report_items', report, i=i,
+                                                         preamble_values={'report_id': report_id})
+            # print(insert_string, values)
+            cursor.execute(insert_string, values)
+        connection.commit()
+      except AttributeError:
+        pass
 
 config = configparser.ConfigParser()
 if args.config_file: