[Branch,~linaro-validation/lava-scheduler/trunk] Rev 87: Make errors when submitting jobs easier to understand

Message ID 20111020170614.23742.62979.launchpad@ackee.canonical.com
State Accepted
Headers show

Commit Message

Paul Larson Oct. 20, 2011, 5:06 p.m.
Merge authors:
  Michael Hudson-Doyle (mwhudson)
Related merge proposals:
  https://code.launchpad.net/~mwhudson/lava-scheduler/helpful-submit-job-messages-bug-861998/+merge/79762
  proposed by: Michael Hudson-Doyle (mwhudson)
  review: Approve - Zygmunt Krynicki (zkrynicki)
------------------------------------------------------------
revno: 87 [merge]
committer: Paul Larson <paul.larson@canonical.com>
branch nick: lava-scheduler
timestamp: Thu 2011-10-20 12:03:51 -0500
message:
  Make errors when submitting jobs easier to understand
modified:
  lava_scheduler_app/api.py
  lava_scheduler_app/models.py


--
lp:lava-scheduler
https://code.launchpad.net/~linaro-validation/lava-scheduler/trunk

You are subscribed to branch lp:lava-scheduler.
To unsubscribe from this branch go to https://code.launchpad.net/~linaro-validation/lava-scheduler/trunk/+edit-subscription

Patch

=== modified file 'lava_scheduler_app/api.py'
--- lava_scheduler_app/api.py	2011-08-19 03:40:59 +0000
+++ lava_scheduler_app/api.py	2011-10-19 21:05:42 +0000
@@ -1,8 +1,15 @@ 
 import xmlrpclib
 
+from simplejson import JSONDecodeError
+
 from linaro_django_xmlrpc.models import ExposedAPI
 
-from lava_scheduler_app.models import TestJob
+from lava_scheduler_app.models import (
+    Device,
+    DeviceType,
+    JSONDataError,
+    TestJob,
+    )
 
 
 class SchedulerAPI(ExposedAPI):
@@ -12,7 +19,17 @@ 
             raise xmlrpclib.Fault(401, "Authentication required.")
         if not self.user.has_perm('lava_scheduler_app.add_testjob'):
             raise xmlrpclib.Fault(403, "Permission denied.")
-        return TestJob.from_json_and_user(job_data, self.user).id
+        try:
+            job = TestJob.from_json_and_user(job_data, self.user)
+        except JSONDecodeError as e:
+            raise xmlrpclib.Fault(400, "Decoding JSON failed: %s." % e)
+        except JSONDataError as e:
+            raise xmlrpclib.Fault(400, str(e))
+        except Device.DoesNotExist:
+            raise xmlrpclib.Fault(404, "Specified device not found.")
+        except DeviceType.DoesNotExist:
+            raise xmlrpclib.Fault(404, "Specified device type not found.")
+        return job.id
 
     def cancel_job(self, job_id):
         if not self.user:

=== modified file 'lava_scheduler_app/models.py'
--- lava_scheduler_app/models.py	2011-10-06 03:06:09 +0000
+++ lava_scheduler_app/models.py	2011-10-19 20:45:42 +0000
@@ -1,10 +1,14 @@ 
-import json
+import simplejson
 
 from django.contrib.auth.models import User
 from django.db import models
 from django.utils.translation import ugettext as _
 
 
+class JSONDataError(ValueError):
+    """Error raised when JSON is syntactically valid but ill-formed."""
+
+
 class DeviceType(models.Model):
     """
     A class of device, for example a pandaboard or a snowball.
@@ -167,13 +171,16 @@ 
 
     @classmethod
     def from_json_and_user(cls, json_data, user):
-        job_data = json.loads(json_data)
+        job_data = simplejson.loads(json_data)
         if 'target' in job_data:
             target = Device.objects.get(hostname=job_data['target'])
             device_type = None
-        else:
+        elif 'device_type' in job_data:
             target = None
             device_type = DeviceType.objects.get(name=job_data['device_type'])
+        else:
+            raise JSONDataError(
+                "Neither 'target' nor 'device_type' found in job data.")
         job_name = job_data.get('job_name', '')
         job = TestJob(
             definition=json_data, submitter=user, requested_device=target,