Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions src/jsonata/jexception.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,20 @@ class JException(RuntimeError):
location: int
current: Optional[Any]
expected: Optional[Any]
extra: Optional[Any]

type: Optional[str]

# remaining: Sequence[tokenizer.Tokenizer.Token] | None

def __init__(self, error, location=0, current_token=None, expected=None):
super().__init__(JException.msg(error, location, current_token, expected))
def __init__(self, error, location=0, current_token=None, expected=None, extra=None):
args = (current_token, expected) if extra is None else (current_token, expected, extra)
super().__init__(JException.msg(error, location, *args))
self.error = error
self.location = location
self.current = current_token
self.expected = expected
self.extra = extra

self.type = None
self.remaining = None
Expand Down Expand Up @@ -79,7 +82,7 @@ def get_expected(self) -> Optional[Any]:
# @return
#
def get_detailed_error_message(self) -> str:
return JException.msg(self.error, self.location, self.current, self.expected, True)
return JException.msg(self.error, self.location, self.current, self.expected, self.extra, details=True)

#
# Generate error message from given error code
Expand All @@ -95,23 +98,23 @@ def get_detailed_error_message(self) -> str:
# @return
#
@staticmethod
def msg(error: str, location: int, arg1: Optional[Any], arg2: Optional[Any], details: bool = False) -> str:
def msg(error: str, location: int, *args: Any, details: bool = False) -> str:
Comment thread
rayokota marked this conversation as resolved.
message = JException.error_codes.get(error)

if message is None:
# unknown error code
return "JSonataException " + str(error) + (
" {code=unknown position=" + str(location) + " arg1=" + arg1 + " arg2=" + arg2 + "}" if details else "")
" {code=unknown position=" + str(location) + " arg1=" + str(args[0] if len(args) > 0 else None) + " arg2=" + str(args[1] if len(args) > 1 else None) + "}" if details else "")

formatted = message

if formatted == "{{{message}}}":
return str(arg1)
return str(args[0]) if args else ""

# Replace any {{var}} with format "{}"
formatted = re.sub("\\{\\{\\w+\\}\\}", "{}", formatted)

formatted = formatted.format(arg1, arg2)
formatted = formatted.format(*args)

if details:
formatted = formatted + " {code=" + error
Expand Down
2 changes: 1 addition & 1 deletion src/jsonata/jsonata.py
Original file line number Diff line number Diff line change
Expand Up @@ -857,7 +857,7 @@ def evaluate_comparison_expression(self, lhs: Optional[Any], rhs: Optional[Any],

else:

raise jexception.JException("T2009", 0, lhs, rhs)
raise jexception.JException("T2009", 0, lhs, rhs, op)
Comment thread
rayokota marked this conversation as resolved.

if op == "<":
result = lhs < rhs
Expand Down
2 changes: 1 addition & 1 deletion src/jsonata/signature.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ def validate(self, args: Any, context: Optional[Any]) -> Optional[Any]:
array_ok = False
break
if not array_ok:
raise jexception.JException("T0412", -1, arg, param.subtype)
raise jexception.JException("T0412", -1, arg_index + 1, self.function_name, param.subtype)
# the function expects an array. If it's not one, make it so
if single != "a":
arg = [arg]
Expand Down
Loading