Skip to content

Commit 27cd35a

Browse files
committed
Very simple exception handling + some doc fixes
1 parent 0cdd97e commit 27cd35a

File tree

5 files changed

+32
-15
lines changed

5 files changed

+32
-15
lines changed

elasticsearch_dsl/exceptions.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class ElasticsearchDslException(Exception):
2+
pass
3+
4+
5+
class UnknownDslObject(ElasticsearchDslException):
6+
pass

elasticsearch_dsl/filter.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,17 @@ def F(name_or_filter, **params):
99
# {"term": {...}}
1010
if isinstance(name_or_filter, dict):
1111
if params:
12-
raise #XXX
12+
raise ValueError('F() cannot accept parameters when passing in a dict.')
1313
if len(name_or_filter) != 1:
14-
raise #XXX
14+
raise ValueError('F() can only accept dict with a single filter ({"bool": {...}}). '
15+
'Instead it got (%r)' % name_or_filter)
1516
name, params = name_or_filter.copy().popitem()
1617
return Filter.get_dsl_class(name)(**params)
1718

1819
# Term(...)
1920
if isinstance(name_or_filter, Filter):
2021
if params:
21-
raise #XXX
22+
raise ValueError('F() cannot accept parameters when passing in a Filter object.')
2223
return name_or_filter
2324

2425
# 'term', tag='python', ...

elasticsearch_dsl/query.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,17 @@ def Q(name_or_query, **params):
1010
# {"match": {"title": "python"}}
1111
if isinstance(name_or_query, dict):
1212
if params:
13-
raise #XXX
13+
raise ValueError('Q() cannot accept parameters when passing in a dict.')
1414
if len(name_or_query) != 1:
15-
raise #XXX
15+
raise ValueError('Q() can only accept dict with a single query ({"match": {...}}). '
16+
'Instead it got (%r)' % name_or_query)
1617
name, params = name_or_query.copy().popitem()
1718
return Query.get_dsl_class(name)(**params)
1819

1920
# MatchAll()
2021
if isinstance(name_or_query, Query):
2122
if params:
22-
raise #XXX
23+
raise ValueError('Q() cannot accept parameters when passing in a Query object.')
2324
return name_or_query
2425

2526
# "match", title="python"

elasticsearch_dsl/search.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ def __getitem__(self, n):
104104
if isinstance(n, slice):
105105
# If negative slicing, abort.
106106
if n.start and n.start < 0 or n.stop and n.stop < 0:
107-
raise #XXX
107+
raise ValueError("Search does not support negative slicing.")
108108
# Elasticsearch won't get all results so we default to size: 10 if
109109
# stop not given.
110110
s._extra['from'] = n.start or 0
@@ -113,7 +113,7 @@ def __getitem__(self, n):
113113
else: # This is an index lookup, equivalent to slicing by [n:n+1].
114114
# If negative index, abort.
115115
if n < 0:
116-
raise #XXX
116+
raise ValueError("Search does not support negative indexing.")
117117
s._extra['from'] = n
118118
s._extra['size'] = 1
119119
return s
@@ -249,12 +249,15 @@ def doc_type(self, *doc_type):
249249
s._doc_type = (self._doc_type or []) + list(doc_type)
250250
return s
251251

252-
def to_dict(self, count=False):
252+
def to_dict(self, count=False, **kwargs):
253253
"""
254-
Serialize the search into the dictionary that will be sent over as the request's body.
254+
Serialize the search into the dictionary that will be sent over as the
255+
request's body.
255256
256257
:arg count: a flag to specify we are interested in a body for count -
257258
no aggregations, no pagination bounds etc.
259+
260+
All additional keyword arguments will be included into the dictionary.
258261
"""
259262
if self.filter:
260263
d = {
@@ -279,6 +282,7 @@ def to_dict(self, count=False):
279282

280283
if not count:
281284
d.update(self._extra)
285+
d.update(kwargs)
282286
return d
283287

284288
def using(self, client):

elasticsearch_dsl/utils.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
from six import iteritems
44

5+
from .exceptions import UnknownDslObject
6+
57
def _wrap(val):
68
if isinstance(val, dict):
79
return AttrDict(val)
@@ -55,7 +57,8 @@ def __getattr__(self, attr_name):
5557
try:
5658
return _wrap(self._d_[attr_name])
5759
except KeyError:
58-
raise AttributeError()
60+
raise AttributeError(
61+
'%r object has no attribute %r' % (self.__class__.__name__, attr_name))
5962

6063
def __getitem__(self, key):
6164
# don't wrap things whe accessing via __getitem__ for consistency
@@ -143,7 +146,8 @@ def _setattr(self, name, value):
143146

144147
def __getattr__(self, name):
145148
if name.startswith('_'):
146-
raise AttributeError()
149+
raise AttributeError(
150+
'%r object has no attribute %r' % (self.__class__.__name__, name))
147151

148152
value = None
149153
try:
@@ -158,7 +162,8 @@ def __getattr__(self, name):
158162
elif pinfo.get('hash'):
159163
value = self._params.setdefault(name, {})
160164
if value is None:
161-
raise AttributeError()
165+
raise AttributeError(
166+
'%r object has no attribute %r' % (self.__class__.__name__, name))
162167

163168
# wrap nested dicts in AttrDict for convenient access
164169
if isinstance(value, dict):
@@ -257,14 +262,14 @@ def get_dsl_type(cls, name):
257262
try:
258263
return cls._types[name]
259264
except KeyError:
260-
raise #XXX
265+
raise UnknownDslObject('DSL type %s does not exist.' % name)
261266

262267
@classmethod
263268
def get_dsl_class(cls, name):
264269
try:
265270
return cls._classes[name]
266271
except KeyError:
267-
raise #XXX
272+
raise UnknownDslObject('DSL class %s does not exist in %s.' % (name, self._type_name))
268273

269274

270275
class BoolMixin(object):

0 commit comments

Comments
 (0)