Skip to content
Open
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
20 changes: 11 additions & 9 deletions src/jsonata/jexception.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,19 @@ 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, *args: Any):
super().__init__(JException.msg(error, location, *args))
Comment on lines +39 to +40
self.error = error
self.location = location
self.current = current_token
self.expected = expected
self.current = args[0] if len(args) > 0 else None
self.expected = args[1] if len(args) > 1 else None
self.extra = args[2] if len(args) > 2 else None

self.type = None
self.remaining = None
Expand Down Expand Up @@ -79,7 +81,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 +97,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:
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, op, rhs)

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