Skip to content

[Code Quality] Extract repetitive error formatting into helper function #13693

@github-actions

Description

@github-actions

Description

The compiler.go file contains repetitive error formatting code that appears ~20 times throughout the file. This pattern is duplicated with minimal variation, making the file harder to maintain and unnecessarily long.

Identified in: Daily Compiler Code Quality Report #12164

Current Pattern (Repeated ~20 times)

formattedErr := console.FormatError(console.CompilerError{
    Position: console.ErrorPosition{
        File:   markdownPath,
        Line:   1,
        Column: 1,
    },
    Type:    "error",
    Message: err.Error(),
})
return errors.New(formattedErr)

Impact

  • Code duplication: ~200-300 lines of repetitive code
  • Maintainability: Changes to error format require updating 20+ locations
  • Readability: Obscures the actual validation logic

Suggested Changes

1. Create Helper Function

// formatCompilerError creates a formatted compiler error with file position context
func formatCompilerError(markdownPath string, err error) error {
    formattedErr := console.FormatError(console.CompilerError{
        Position: console.ErrorPosition{
            File:   markdownPath,
            Line:   1,
            Column: 1,
        },
        Type:    "error",
        Message: err.Error(),
    })
    return errors.New(formattedErr)
}

// Optional: Version with custom line/column
func formatCompilerErrorAt(markdownPath string, line, column int, err error) error {
    formattedErr := console.FormatError(console.CompilerError{
        Position: console.ErrorPosition{
            File:   markdownPath,
            Line:   line,
            Column: column,
        },
        Type:    "error",
        Message: err.Error(),
    })
    return errors.New(formattedErr)
}

2. Replace All Occurrences

Search and replace pattern:

Find:

formattedErr := console.FormatError\\(console.CompilerError\\{[^}]+\\}\\)\\s+return errors.New\\(formattedErr\\)

Replace with:

return formatCompilerError(markdownPath, err)

3. Add Tests

func TestFormatCompilerError(t *testing.T) {
    err := formatCompilerError("test.md", errors.New("test error"))
    assert.Contains(t, err.Error(), "test.md")
    assert.Contains(t, err.Error(), "test error")
}

Files Affected

  • pkg/workflow/compiler.go (661 lines → ~450 lines estimated)

Success Criteria

  • Helper function formatCompilerError() created
  • All 20+ occurrences replaced with helper call
  • Tests added for helper function
  • All existing tests pass
  • File size reduced by ~200 lines
  • No change in runtime behavior

Benefits

  • Reduces code duplication by ~200 lines
  • Improves maintainability - single source of truth for error formatting
  • Easier to enhance - can add features (e.g., error codes) in one place
  • Better readability - less noise in validation logic

Estimated Effort

30-60 minutes - Low complexity, high impact refactoring

Source

Extracted from Daily Compiler Code Quality Report discussion #12164

Priority: Medium - Code quality improvement with significant impact

AI generated by Discussion Task Miner - Code Quality Improvement Agent

  • expires on Feb 18, 2026, 9:13 AM UTC

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions