Skip to content

feat: improve error UX with suggestions, context, and source snippets#70

Merged
alvinreal merged 1 commit intomainfrom
fix/issue-31
Feb 22, 2026
Merged

feat: improve error UX with suggestions, context, and source snippets#70
alvinreal merged 1 commit intomainfrom
fix/issue-31

Conversation

@alvinreal
Copy link
Owner

Polishes all error messages to be clear, actionable, and include context.

Changes

Fuzzy Suggestions

  • Unknown format names suggest the closest match via Levenshtein distance
    • morph -f jsnerror: unknown input format: 'jsn'. Did you mean 'json'?
    • morph -t ymalerror: unknown output format: 'ymal'. Did you mean 'yml'?
  • Unknown function names suggest the closest match
    • set .x = lowr(.x)error: unknown function: lowr. Did you mean 'lower'?

Improved Cast Errors

  • Cast errors now include: type name, value preview, path, and expected types
    • cannot cast array to int at .items: got [1, 2, 3], expected int-compatible type (int, float, string, bool, null)

Pretty Error Formatting

  • pretty_print() method on MorphError for human-friendly stderr output
  • Source snippets with line numbers and caret pointing at error column:
    error: unexpected token
      |
    2 | line 2 has error
      |     ^
    
  • File errors include the full attempted path
  • Parse errors show line:column location

Other Improvements

  • Display impl for Path AST node (readable .users.[0].name format)
  • main.rs uses pretty_print() for all error output

Implementation

  • Levenshtein edit distance (single-row DP, no external deps)
  • suggest_closest() generic fuzzy matcher (case-insensitive, max distance threshold)
  • suggest_format() covers all format names/aliases
  • suggest_function() covers all 50+ function names/aliases
  • format_source_snippet() for source-annotated error display

Tests

  • 16 new unit tests: edit distance, format/function suggestions, pretty printing, source snippets
  • 6 new integration tests: CLI error UX for unknown formats, unknown functions, parse errors, file not found, cast errors

Fixes #31

Polishes all error messages to be clear, actionable, and contextual:

- Unknown format names suggest closest match via Levenshtein distance
  (e.g., 'jsn' → Did you mean 'json'?)
- Unknown function names suggest closest match
  (e.g., 'lowr' → Did you mean 'lower'?)
- Cast errors include type name, value preview, path, and expected types
- File errors include the full attempted path
- Parse errors show line:column location
- Pretty error formatter with source snippets showing line numbers
  and caret (^) pointing at the error column
- main.rs uses pretty_print() for human-friendly stderr output
- Path Display impl for readable error paths (.users.[0].name)

Includes:
- Levenshtein edit distance implementation (single-row DP)
- suggest_closest() generic fuzzy matcher
- suggest_format() and suggest_function() with all known names
- format_source_snippet() for source-annotated errors
- 16 new unit tests for suggestions, pretty printing, edit distance
- 6 new integration tests for CLI error UX

Fixes #31
@alvinreal alvinreal merged commit 0c5f894 into main Feb 22, 2026
6 checks passed
@alvinreal alvinreal deleted the fix/issue-31 branch February 22, 2026 16:39
@github-actions github-actions bot mentioned this pull request Mar 20, 2026
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.

Error UX: messages, suggestions, and context

1 participant