From patchwork Thu Oct 20 17:06:14 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul Larson X-Patchwork-Id: 4753 Return-Path: X-Original-To: patchwork@peony.canonical.com Delivered-To: patchwork@peony.canonical.com Received: from fiordland.canonical.com (fiordland.canonical.com [91.189.94.145]) by peony.canonical.com (Postfix) with ESMTP id 304F923EF7 for ; Thu, 20 Oct 2011 17:06:16 +0000 (UTC) Received: from mail-ey0-f180.google.com (mail-ey0-f180.google.com [209.85.215.180]) by fiordland.canonical.com (Postfix) with ESMTP id 1CE03A18188 for ; Thu, 20 Oct 2011 17:06:16 +0000 (UTC) Received: by eyg5 with SMTP id 5so3912753eyg.11 for ; Thu, 20 Oct 2011 10:06:16 -0700 (PDT) Received: by 10.223.17.3 with SMTP id q3mr19301436faa.28.1319130375876; Thu, 20 Oct 2011 10:06:15 -0700 (PDT) X-Forwarded-To: linaro-patchwork@canonical.com X-Forwarded-For: patch@linaro.org linaro-patchwork@canonical.com Delivered-To: patches@linaro.org Received: by 10.152.1.71 with SMTP id 7cs129927lak; Thu, 20 Oct 2011 10:06:15 -0700 (PDT) Received: by 10.227.42.71 with SMTP id r7mr1257699wbe.105.1319130375191; Thu, 20 Oct 2011 10:06:15 -0700 (PDT) Received: from indium.canonical.com (indium.canonical.com. [91.189.90.7]) by mx.google.com with ESMTPS id ge19si7495772wbb.123.2011.10.20.10.06.14 (version=TLSv1/SSLv3 cipher=OTHER); Thu, 20 Oct 2011 10:06:15 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of bounces@canonical.com designates 91.189.90.7 as permitted sender) client-ip=91.189.90.7; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of bounces@canonical.com designates 91.189.90.7 as permitted sender) smtp.mail=bounces@canonical.com Received: from ackee.canonical.com ([91.189.89.26]) by indium.canonical.com with esmtp (Exim 4.71 #1 (Debian)) id 1RGw4M-0007i6-Kq for ; Thu, 20 Oct 2011 17:06:14 +0000 Received: from ackee.canonical.com (localhost [127.0.0.1]) by ackee.canonical.com (Postfix) with ESMTP id 92AE1FB48E for ; Thu, 20 Oct 2011 17:06:14 +0000 (UTC) MIME-Version: 1.0 X-Launchpad-Project: lava-scheduler X-Launchpad-Branch: ~linaro-validation/lava-scheduler/trunk X-Launchpad-Message-Rationale: Subscriber X-Launchpad-Branch-Revision-Number: 87 X-Launchpad-Notification-Type: branch-revision To: Linaro Patch Tracker From: noreply@launchpad.net Subject: [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> Date: Thu, 20 Oct 2011 17:06:14 -0000 Reply-To: noreply@launchpad.net Sender: bounces@canonical.com Errors-To: bounces@canonical.com Precedence: bulk X-Generated-By: Launchpad (canonical.com); Revision="14165"; Instance="launchpad-lazr.conf" X-Launchpad-Hash: 68bed2cb6acfea6ac4bcdcf423b10163427d496e 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 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 === 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,