Resolve internal errors 500 37/61337/1
authorStamatis Katsaounis <mokats@intracom-telecom.com>
Fri, 24 Aug 2018 13:57:38 +0000 (16:57 +0300)
committerStamatis Katsaounis <mokats@intracom-telecom.com>
Fri, 24 Aug 2018 13:57:38 +0000 (16:57 +0300)
* Check that a user uploaded a file and return appropriate error if not
* Check that a file is a valid gzip file and return appropriate error if not

Change-Id: I17dd1ee459d06687156b7fd36f27353325a0b737
Signed-off-by: Stamatis Katsaounis <mokats@intracom-telecom.com>
opnfv_testapi/resources/result_handlers.py

index 1f6ec73..b7b5c2b 100644 (file)
@@ -245,9 +245,19 @@ class ResultsUploadHandler(ResultsCLHandler):
             @raise 404: pod/project/testcase not exist
             @raise 400: body/pod_name/project_name/case_name not provided
         """
-        fileinfo = self.request.files['file'][0]
-        tar_in = tarfile.open(fileobj=io.BytesIO(fileinfo['body']),
-                              mode="r:gz")
+        file_array = self.request.files.get('file', None)
+        if file_array is None:
+            msg = 'Please upload a file.'
+            self.finish_request({'code': 403, 'msg': msg})
+            return
+        fileinfo = file_array[0]
+        try:
+            tar_in = tarfile.open(fileobj=io.BytesIO(fileinfo['body']),
+                                  mode="r:gz")
+        except tarfile.ReadError:
+            msg = 'Please upload a valid gzip file.'
+            self.finish_request({'code': 403, 'msg': msg})
+            return
         try:
             results = tar_in.extractfile('results/results.json').read()
         except KeyError: