[PATCH 5/8] xmlrpc: catch possible exceptions in patch filtering.
Stephen Finucane
stephenfinucane at hotmail.com
Sun Sep 4 06:22:23 AEST 2016
On 03 Sep 17:07, Daniel Axtens wrote:
> Currently, filtering by project, submitter, delegate or state uses a
> filter(id=filt[key])[0]. This will throw an exception when something
> isn't found, as filter will return [], and getting the first element of
> that will fail.
>
> Convert them to explicit get()s, so it's clearer that they can throw an
> exception, then catch the 3 possible types of DoesNotExists exceptions.
>
> Signed-off-by: Daniel Axtens <dja at axtens.net>
One change below, which I'll make when merging.
Reviewed-by: Stephen Finucane <stephenfinucane at hotmail.com>
> ---
> patchwork/views/xmlrpc.py | 28 ++++++++++++++++------------
> 1 file changed, 16 insertions(+), 12 deletions(-)
>
> diff --git a/patchwork/views/xmlrpc.py b/patchwork/views/xmlrpc.py
> index 489c87b74023..c5e7b9078f15 100644
> --- a/patchwork/views/xmlrpc.py
> +++ b/patchwork/views/xmlrpc.py
> @@ -582,18 +582,22 @@ def patch_list(filt=None):
> # Invalid lookup type given
> return []
>
> - if parts[0] == 'project_id':
> - dfilter['project'] = Project.objects.filter(id=filt[key])[0]
> - elif parts[0] == 'submitter_id':
> - dfilter['submitter'] = Person.objects.filter(id=filt[key])[0]
> - elif parts[0] == 'delegate_id':
> - dfilter['delegate'] = Person.objects.filter(id=filt[key])[0]
> - elif parts[0] == 'state_id':
> - dfilter['state'] = State.objects.filter(id=filt[key])[0]
> - elif parts[0] == 'max_count':
> - max_count = filt[key]
> - else:
> - dfilter[key] = filt[key]
> + try:
> + if parts[0] == 'project_id':
> + dfilter['project'] = Project.objects.get(id=filt[key])
> + elif parts[0] == 'submitter_id':
> + dfilter['submitter'] = Person.objects.get(id=filt[key])
> + elif parts[0] == 'delegate_id':
> + dfilter['delegate'] = Person.objects.get(id=filt[key])
> + elif parts[0] == 'state_id':
> + dfilter['state'] = State.objects.get(id=filt[key])
> + elif parts[0] == 'max_count':
> + max_count = filt[key]
> + else:
> + dfilter[key] = filt[key]
> + except (Project.DoesNotExist, Person.DoesNotExist, State.DoesNotExist):
We can just capture the generic DoesNotExist exception here.
> + # Invalid Project, Person or State given
> + return []
>
> patches = Patch.objects.filter(**dfilter)
>
> --
> 2.7.4
>
> _______________________________________________
> Patchwork mailing list
> Patchwork at lists.ozlabs.org
> https://lists.ozlabs.org/listinfo/patchwork
More information about the Patchwork
mailing list