utt icon indicating copy to clipboard operation
utt copied to clipboard

Improve error handling for invalid date input

Open loganthomas opened this issue 1 month ago • 0 comments

Improve error messages for invalid dates

Closes #94

Summary

When utt encounters invalid date input—either from command-line arguments or from entries in the log file—it now displays a user-friendly error message instead of a Python traceback.

Problem

Previously, invalid date usage produced unhelpful tracebacks:

$ utt report 324-4242-43
Traceback (most recent call last):
  File "/opt/homebrew/bin/utt", line 8, in <module>
    sys.exit(main())
  ...
  File "/opt/homebrew/lib/python3.9/site-packages/utt/components/report_args.py", line 74, in parse_absolute_date
    return datetime.datetime.strptime(datestring, "%Y-%m-%d").date()
  ...
ValueError: time data '324-4242-43' does not match format '%Y-%m-%d'

Solution

Added a simple UttError exception class for user-facing errors. Invalid dates now produce clean, actionable messages.

Examples

Invalid date argument

$ utt report invalid-date
error: Invalid date: invalid-date (expected YYYY-MM-DD)

Invalid month argument

$ utt report --month invalid
error: Invalid month: invalid (expected YYYY-MM)

Invalid date in log file

$ utt report
error: Invalid date at line 14: 2025-27-27 17:00 misc: testing

Entries not in chronological order

$ utt report
error: Line 14 not in chronological order: 2025-11-26 17:00 misc: testing

Changes

  • utt/components/entries.py: Added UttError exception class; catch ValueError from date parsing and convert to user-friendly error
  • utt/components/report_args.py: Wrap strptime calls to raise UttError on invalid input
  • utt/__main__.py: Catch UttError at top level and print clean error message

Tests

Added tests for the new error handling:

  • test_invalid_date_raises_utt_error
  • test_invalid_month_raises_utt_error
  • test_invalid_date_raises_value_error (for log file entries)

loganthomas avatar Nov 28 '25 17:11 loganthomas