1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
|
from jmespath.compat import with_str_method
class JMESPathError(ValueError):
pass
@with_str_method
class ParseError(JMESPathError):
_ERROR_MESSAGE = 'Invalid jmespath expression'
def __init__(self, lex_position, token_value, token_type,
msg=_ERROR_MESSAGE):
super(ParseError, self).__init__(lex_position, token_value, token_type)
self.lex_position = lex_position
self.token_value = token_value
self.token_type = token_type.upper()
self.msg = msg
# Whatever catches the ParseError can fill in the full expression
self.expression = None
def __str__(self):
# self.lex_position +1 to account for the starting double quote char.
underline = ' ' * (self.lex_position + 1) + '^'
return (
'%s: Parse error at column %s, '
'token "%s" (%s), for expression:\n"%s"\n%s' % (
self.msg, self.lex_position, self.token_value, self.token_type,
self.expression, underline))
@with_str_method
class IncompleteExpressionError(ParseError):
def set_expression(self, expression):
self.expression = expression
self.lex_position = len(expression)
self.token_type = None
self.token_value = None
def __str__(self):
# self.lex_position +1 to account for the starting double quote char.
underline = ' ' * (self.lex_position + 1) + '^'
return (
'Invalid jmespath expression: Incomplete expression:\n'
'"%s"\n%s' % (self.expression, underline))
@with_str_method
class LexerError(ParseError):
def __init__(self, lexer_position, lexer_value, message, expression=None):
self.lexer_position = lexer_position
self.lexer_value = lexer_value
self.message = message
super(LexerError, self).__init__(lexer_position,
lexer_value,
message)
# Whatever catches LexerError can set this.
self.expression = expression
def __str__(self):
underline = ' ' * self.lexer_position + '^'
return 'Bad jmespath expression: %s:\n%s\n%s' % (
self.message, self.expression, underline)
@with_str_method
class ArityError(ParseError):
def __init__(self, expected, actual, name):
self.expected_arity = expected
self.actual_arity = actual
self.function_name = name
self.expression = None
def __str__(self):
return ("Expected %s %s for function %s(), "
"received %s" % (
self.expected_arity,
self._pluralize('argument', self.expected_arity),
self.function_name,
self.actual_arity))
def _pluralize(self, word, count):
if count == 1:
return word
else:
return word + 's'
@with_str_method
class VariadictArityError(ArityError):
def __str__(self):
return ("Expected at least %s %s for function %s(), "
"received %s" % (
self.expected_arity,
self._pluralize('argument', self.expected_arity),
self.function_name,
self.actual_arity))
@with_str_method
class JMESPathTypeError(JMESPathError):
def __init__(self, function_name, current_value, actual_type,
expected_types):
self.function_name = function_name
self.current_value = current_value
self.actual_type = actual_type
self.expected_types = expected_types
def __str__(self):
return ('In function %s(), invalid type for value: %s, '
'expected one of: %s, received: "%s"' % (
self.function_name, self.current_value,
self.expected_types, self.actual_type))
class EmptyExpressionError(JMESPathError):
def __init__(self):
super(EmptyExpressionError, self).__init__(
"Invalid JMESPath expression: cannot be empty.")
class UnknownFunctionError(JMESPathError):
pass
|