Skip to content

Commit b4e1ef7

Browse files
committed
Merge branch 'develop' into feature/vnext-preview
2 parents 3948d35 + f324de9 commit b4e1ef7

File tree

3 files changed

+29
-22
lines changed

3 files changed

+29
-22
lines changed

bin/jp.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ def main():
4242
except exceptions.JMESPathTypeError as e:
4343
sys.stderr.write("invalid-type: %s\n" % e)
4444
return 1
45+
except exceptions.JMESPathValueError as e:
46+
sys.stderr.write("invalid-value: %s\n" % e)
47+
return 1
4548
except exceptions.UnknownFunctionError as e:
4649
sys.stderr.write("unknown-function: %s\n" % e)
4750
return 1

jmespath/exceptions.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,19 @@ def __str__(self):
112112
self.expected_types, self.actual_type))
113113

114114

115+
@with_str_method
116+
class JMESPathValueError(JMESPathError):
117+
def __init__(self, function_name, current_value, expected_types):
118+
self.function_name = function_name
119+
self.current_value = current_value
120+
self.expected_types = expected_types
121+
122+
def __str__(self):
123+
return ('In function %s(), invalid value: "%s", '
124+
'expected: %s"%s"' % (
125+
self.function_name, self.current_value,
126+
self.expected_types))
127+
115128
class EmptyExpressionError(JMESPathError):
116129
def __init__(self):
117130
super(EmptyExpressionError, self).__init__(

jmespath/functions.py

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
from jmespath.compat import iteritems
1111
from jmespath.compat import map
1212
from jmespath.compat import string_type as STRING_TYPE
13-
from jmespath.compat import get_methods
1413

1514

1615
# python types -> jmespath types
@@ -336,8 +335,8 @@ def _func_keys(self, arg):
336335
{'type': 'number', 'optional': True},
337336
{'type': 'number', 'optional': True})
338337
def _func_find_first(self, text, search, start = 0, end = None):
339-
self._ensure_integer('find_first', start, start)
340-
self._ensure_integer('find_first', end, end)
338+
self._ensure_integer('find_first', 'start', start)
339+
self._ensure_integer('find_first', 'end', end)
341340
return self._find_impl(
342341
text,
343342
search,
@@ -352,8 +351,8 @@ def _func_find_first(self, text, search, start = 0, end = None):
352351
{'type': 'number', 'optional': True},
353352
{'type': 'number', 'optional': True})
354353
def _func_find_last(self, text, search, start = 0, end = None):
355-
self._ensure_integer('find_last', start, start)
356-
self._ensure_integer('find_last', end, end)
354+
self._ensure_integer('find_last', 'start', start)
355+
self._ensure_integer('find_last', 'end', end)
357356
return self._find_impl(
358357
text,
359358
search,
@@ -389,7 +388,7 @@ def _func_pad_left(self, text, width, padding = ' '):
389388
{'type': 'number'},
390389
{'type': 'string', 'optional': True})
391390
def _func_pad_right(self, text, width, padding = ' '):
392-
self._ensure_non_negative_integer('pad_left', 'width', width)
391+
self._ensure_non_negative_integer('pad_right', 'width', width)
393392
return self._pad_impl(lambda : text.ljust(width, padding), padding)
394393

395394
def _pad_impl(self, func, padding):
@@ -446,14 +445,10 @@ def _ensure_integer(
446445

447446
if param_value != None:
448447
if int(param_value) != param_value:
449-
raise exceptions.JMESPathError(
450-
'invalid-type: {}() expects ${} to be a '
451-
'integer, but received {} instead.'
452-
.format(
453-
func_name,
454-
param_name,
455-
param_value
456-
))
448+
raise exceptions.JMESPathValueError(
449+
func_name,
450+
param_value,
451+
"integer")
457452

458453
def _ensure_non_negative_integer(
459454
self,
@@ -463,14 +458,10 @@ def _ensure_non_negative_integer(
463458

464459
if param_value != None:
465460
if int(param_value) != param_value or int(param_value) < 0:
466-
raise exceptions.JMESPathError(
467-
'invalid-type: {}() expects ${} to be a '
468-
'non-negative integer, but received {} instead.'
469-
.format(
470-
func_name,
471-
param_name,
472-
param_value
473-
))
461+
raise exceptions.JMESPathValueError(
462+
func_name,
463+
param_name,
464+
"non-negative integer")
474465

475466
@signature({'type': 'string'}, {'type': 'string', 'optional': True})
476467
def _func_trim(self, text, chars = None):

0 commit comments

Comments
 (0)