Skip to content

fix: IndexError when raising T0412/T2009 JExceptions with 3 format args - #43

Open
abhi-0203 wants to merge 1 commit into
rayokota:masterfrom
abhi-0203:fix/jexception-indexerror
Open

abhi-0203 wants to merge 1 commit into
rayokota:masterfrom
abhi-0203:fix/jexception-indexerror

Conversation

@abhi-0203

Copy link
Copy Markdown

Summary

Fix IndexError: Replacement index 2 out of range for positional args tuple when raising T0412 or T2009 JExceptions.

Root Cause

JException.msg() had a fixed signature (error, location, arg1, arg2) but T0412 and T2009 error templates have 3 placeholders:

  • T0412: "Argument {{index}} of Object {{token}} must be an array of {{type}}"
  • T2009: "The values {{value}} and {{value2}} either side of operator {{token}} must be of the same data type"

When str.format(arg1, arg2) was called with these templates, Python raised IndexError because the 3rd {} had no corresponding argument.

Fix

  1. Changed JException.msg() to accept *args — variadic, works for 1, 2, 3, or more args
  2. Updated T0412 raise in signature.py to pass 3 args: (arg_index + 1, function_name, param.subtype)
  3. Updated T2009 raise in jsonata.py to pass 3 args: (lhs, op, rhs)
  4. Added JException.extra attribute to store the 3rd argument (needed for get_detailed_error_message())
  5. get_detailed_error_message() now passes all 3 stored args correctly

Test

import jsonata
expr = jsonata.Jsonata("$sum(x)")
expr.evaluate({"x": ["y"]})  # Was: IndexError, Now: JException "Argument 1 of Object sum must be an array of n"
expr2 = jsonata.Jsonata('"foo" > 3')
expr2.evaluate({})  # Was: IndexError, Now: JException "The values foo and > either side of operator 3 must be of the same data type"

Closes #42

T0412 and T2009 error codes have 3 placeholders but msg() only passed
2 args to .format(), causing IndexError on replacement.

Fix: make JException.msg accept *args instead of 2 fixed positional args.
Updated T0412 raise to pass (index, function_name, subtype) instead of
(arg, subtype). Updated T2009 raise to pass (lhs, op, rhs) instead of
(lhs, rhs). Added .extra attribute to JException to store the 3rd arg.

Fixes: rayokota#42

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🟡 Changes recommended

T2009 arguments are misordered, and exceptions using omitted formatting arguments now raise IndexError.

Get a fresh assessment by requesting another Copilot review.

Review effort: Balanced
Findings: 1 High severity · 1 Medium severity · 1 Low severity

Open (3)
What changed in this PR

Fixes three-argument formatting for T0412 and T2009 exceptions.

Changes:

  • Makes JException formatting variadic and stores a third argument.
  • Supplies three formatting values at affected call sites.
  • Updates detailed exception rendering.
File Description
src/​jsonata/​signature.py Supplies T0412 index, function, and subtype.
src/​jsonata/​jsonata.py Supplies T2009 comparison details.
src/​jsonata/​jexception.py Adds variadic formatting and third-argument storage.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread src/jsonata/jexception.py
Comment on lines +39 to +40
def __init__(self, error, location=0, *args: Any):
super().__init__(JException.msg(error, location, *args))
Comment thread src/jsonata/jsonata.py
else:

raise jexception.JException("T2009", 0, lhs, rhs)
raise jexception.JException("T2009", 0, lhs, op, rhs)
Comment thread src/jsonata/jexception.py
#
@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:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

IndexError when trying to raise JException for T0412

2 participants