Skip to content

Enhance Error Handling for Missing Run Statements and Improve Error Message Formatting #39

@lwgray

Description

@lwgray

Current Behavior

When a Think program is missing required run statements or has other structural issues, the error handling is inadequate:

  • Error messages lack line numbers and column information
  • No source code context is provided
  • String formatting errors occur when reporting issues
  • Current error message: Think Error: 'str' object has no attribute 'message'

Expected Behavior

Error handling should:

  1. Properly validate program structure:

    • Check for presence of required run statements
    • Verify all defined tasks are run
    • Ensure all run statements reference valid tasks
  2. Provide detailed error messages with:

    • Precise line and column numbers
    • Relevant source code context
    • Clear explanation of the issue
    • Suggestions for fixing the error
  3. Use proper error classes:

    • ThinkParserError for syntax/structure issues
    • ThinkPyRuntimeError for runtime validation
    • Consistent error formatting

Example

Current error:

Think Error: 'str' object has no attribute 'message' 
Line: None 
Column: None 
Context: Near token: '' 
Source code:

Expected error:

Think Error: Program must contain at least one run statement
Line: 6
Column: 1
Context:
-> 6: step "Calculate":
   7:     score = 95
   8:     print(score)
   9: # Missing run statement should be here

Technical Details

The fix requires:

  1. Implementing a ThinkValidator class for program structure validation
  2. Modifying parser and interpreter error handling to use proper error classes
  3. Adding source code context tracking for error reporting
  4. Improving error message formatting and context generation

Related Files

  • errors.py
  • parser.py
  • interpreter.py

Labels

  • bug
  • error-handling
  • documentation
  • enhancement

Priority

High - This impacts user experience and code understanding significantly.

/cc @project-maintainers

Metadata

Metadata

Assignees

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions