Skip to content

Feat line number in error messages.#455

Draft
SCMusson wants to merge 1 commit intoOpShin:mainfrom
SCMusson:feat_error_pos
Draft

Feat line number in error messages.#455
SCMusson wants to merge 1 commit intoOpShin:mainfrom
SCMusson:feat_error_pos

Conversation

@SCMusson
Copy link
Contributor

@SCMusson SCMusson commented Jun 28, 2025

Targetting #54

Implementing some way to customize error messages such as by adding the lineno of the source code line that caused that error.

This pull request adds a configuration option:
config = opshin.DEFAULT_CONFIG.update(format_error_trace = <custom_error_str>)
where {error} and {line_number} are currently supported placeholders.

For example

from tests.utils import eval_uplc_raw
from opshin import DEFAULT_CONFIG

source_code = """
def validator(x: int) -> int:
    if x > 10:
        y = 5
    return y
"""
config = DEFAULT_CONFIG.update(
    format_error_trace="Error: {error} at line {line_number}"
)
print(eval_uplc_raw(source_code, 4, config=config))

gives:
ComputationResult(result=RuntimeError('Execution called Error'), logs=['Error: NameError: y at line 5'], cost=Budget(cpu=2615762, memory=9166))

TODO

  • Errors in compiler.py supported, errors elsewhere still need updating
  • Unit tests

@SCMusson
Copy link
Contributor Author

@nielstron I have implemented this for NameError and dict KeyError here. If you think this is the right approach, I can extend it to cover the other errors not in opshin/compiler.py.

@nielstron
Copy link
Contributor

nielstron commented Jun 29, 2025

I think this goes in the right direction. However the overall goal is to enable the user to find the location of the bug in their source code quickly so

  • We need to include the file name in the error location (not only the line number). Note that this may be somewhat blocked by Implement a proper import framework #29 and the way imports currently overwrite line numbers / file names
  • I don't understand why exactly the NameError is using the DeadVarOptimizer to locate the access location. Should this not be derivable from the AST node directly at the error location? Overall, the current approach does not look very generalizable and quite confusing that we log line numbers in a somewhat unrelated optimization visitor. It should be clear why and how we store line numbers if they need specific extraction (i.e., by writing a specific line number extractor visitor)

@SCMusson
Copy link
Contributor Author

For your second point about using Code from DeadVarOptimizer. the NameLoadCollector is already used in compiler.py here to find all written variables (all_vs).
I was just trying to avoid creating a clone of NameLoadCollector with a handful of changes. You are correct however, I will create a specific line number extractor visitor in utils.py.

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.

2 participants