Skip to content
This repository has been archived by the owner on Feb 20, 2022. It is now read-only.

Commit

Permalink
Merge pull request milesrichardson#65 from mtsgrd/query_errors
Browse files Browse the repository at this point in the history
Improve error messages.
  • Loading branch information
David Robinson committed Oct 28, 2014
2 parents c540f3c + 967557b commit 1f23538
Showing 1 changed file with 19 additions and 4 deletions.
23 changes: 19 additions & 4 deletions parse_rest/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,21 @@
import collections


class QueryResourceDoesNotExist(Exception):
class QueryError(Exception):
'''Query error base class'''

def __init__(self, message, status_code=None):
super(QueryError, self).__init__(message)
if status_code:
self.status_code = status_code


class QueryResourceDoesNotExist(QueryError):
'''Query returned no results'''
pass


class QueryResourceMultipleResultsReturned(Exception):
class QueryResourceMultipleResultsReturned(QueryError):
'''Query was supposed to return unique result, returned more than one'''
pass

Expand Down Expand Up @@ -163,9 +172,15 @@ def exists(self):
def get(self):
results = self._fetch()
if len(results) == 0:
raise QueryResourceDoesNotExist
error_message = 'Query against %s returned no results' % (
self._manager.model_class.ENDPOINT_ROOT)
raise QueryResourceDoesNotExist(error_message,
status_code=404)
if len(results) >= 2:
raise QueryResourceMultipleResultsReturned
error_message = 'Query against %s returned multiple results' % (
self._manager.model_class.ENDPOINT_ROOT)
raise QueryResourceMultipleResultsReturned(error_message,
status_code=404)
return results[0]

def __repr__(self):
Expand Down

0 comments on commit 1f23538

Please sign in to comment.