diff --git a/src/jsonata/jexception.py b/src/jsonata/jexception.py index 79d09bf..1c51b95 100644 --- a/src/jsonata/jexception.py +++ b/src/jsonata/jexception.py @@ -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)) 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 @@ -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 @@ -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 diff --git a/src/jsonata/jsonata.py b/src/jsonata/jsonata.py index 8248db4..fe01c20 100644 --- a/src/jsonata/jsonata.py +++ b/src/jsonata/jsonata.py @@ -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 diff --git a/src/jsonata/signature.py b/src/jsonata/signature.py index 8e6d60d..5158a46 100644 --- a/src/jsonata/signature.py +++ b/src/jsonata/signature.py @@ -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]