imp: simpler, clearer date parse error messages

When the error message repeated the invalid date at the end,
it was possible to misinterpret that as a suggested fix (reported in chat).

Instead, date errors (most of them) now rely on the highlighted data
excerpt above. This is also preferable since it shows the original
date as written, not a reconstruction with a possibly different format.

Should this be the policy for all error messages going forward ?
It would be easier.
Can we assume the data excerpt is always visible along with the error message ?
It isn't shown by flycheck-hledger in emacs, eg.
This commit is contained in:
Simon Michael 2024-03-26 09:28:31 -10:00
parent 5519f4ac2e
commit fae6e49407
3 changed files with 25 additions and 29 deletions

View File

@ -517,41 +517,39 @@ datep' mYear = do
sep <- datesepchar <?> "date separator" sep <- datesepchar <?> "date separator"
d2 <- decimal <?> "month or day" d2 <- decimal <?> "month or day"
case d1 of case d1 of
Left y -> fullDate startOffset y sep d2 Left y -> fullDate startOffset y sep d2
Right m -> partialDate startOffset mYear m sep d2 Right m -> partialDate startOffset mYear m d2
<?> "full or partial date" <?> "full or partial date"
where where
fullDate :: Int -> Year -> Char -> Month -> TextParser m Day fullDate :: Int -> Year -> Char -> Month -> TextParser m Day
fullDate startOffset year sep1 month = do fullDate startOffset year sep month = do
sep2 <- satisfy isDateSepChar <?> "date separator" sep2 <- satisfy isDateSepChar <?> "date separator"
day <- decimal <?> "day" day <- decimal <?> "day"
endOffset <- getOffset endOffset <- getOffset
let dateStr = show year ++ [sep1] ++ show month ++ [sep2] ++ show day when (sep /= sep2) $
customFailure $ parseErrorAtRegion startOffset endOffset $
when (sep1 /= sep2) $ customFailure $ parseErrorAtRegion startOffset endOffset $ "This date has different separators, please use consistent separators."
"This date is malformed because the separators are different.\n"
++"Please use consistent separators."
case fromGregorianValid year month day of case fromGregorianValid year month day of
Nothing -> customFailure $ parseErrorAtRegion startOffset endOffset $ Nothing ->
"This date is invalid, please correct it: " ++ dateStr customFailure $ parseErrorAtRegion startOffset endOffset $
"This is not a valid date, please fix it."
Just date -> pure $! date Just date -> pure $! date
partialDate :: Int -> Maybe Year -> Month -> Char -> MonthDay -> TextParser m Day partialDate :: Int -> Maybe Year -> Month -> MonthDay -> TextParser m Day
partialDate startOffset myr month sep day = do partialDate startOffset myr month day = do
endOffset <- getOffset endOffset <- getOffset
case myr of case myr of
Just year -> Just year ->
case fromGregorianValid year month day of case fromGregorianValid year month day of
Nothing -> customFailure $ parseErrorAtRegion startOffset endOffset $ Nothing ->
"This date is invalid, please correct it: " ++ dateStr customFailure $ parseErrorAtRegion startOffset endOffset $
"This is not a valid date, please fix it."
Just date -> pure $! date Just date -> pure $! date
where dateStr = show year ++ [sep] ++ show month ++ [sep] ++ show day
Nothing -> customFailure $ parseErrorAtRegion startOffset endOffset $ Nothing ->
"The partial date "++dateStr++" can not be parsed because the current year is unknown.\n" customFailure $ parseErrorAtRegion startOffset endOffset $
++"Consider making it a full date, or add a default year directive.\n" "This partial date can not be parsed because the current year is unknown.\n"
where dateStr = show month ++ [sep] ++ show day ++"Please make it a full date, or add a default year directive."
{-# INLINABLE datep' #-} {-# INLINABLE datep' #-}

View File

@ -1,12 +1,10 @@
# * parseable error $$$ hledger check -f parseable-dates.j
# ** 1. >>>2 /hledger: Error: .*parseable-dates.j:3:1:
$ hledger check -f parseable-dates.j
>2 /hledger: Error: .*parseable-dates.j:3:1:
\| \|
3 \| 2022\/1\/32 3 \| 2022\/1\/32
\| \^\^\^\^\^\^\^\^\^ \| \^\^\^\^\^\^\^\^\^
This date is invalid, please correct it: 2022\/1\/32 This is not a valid date, please fix it.
/ /
>= 1 >>>= 1

View File

@ -8,7 +8,7 @@
a 1 a 1
b b
$ hledger -f- print $ hledger -f- print
>2 /date is invalid/ >2 /is not a valid date/
>= 1 >= 1
# ** 2. too-large day # ** 2. too-large day
@ -17,7 +17,7 @@ $ hledger -f- print
a 1 a 1
b b
$ hledger -f- print $ hledger -f- print
>2 /date is invalid/ >2 /is not a valid date/
>= 1 >= 1
# ** 3. 29th feb on leap year should be ok # ** 3. 29th feb on leap year should be ok
@ -38,7 +38,7 @@ $ hledger -f- print
a 1 a 1
b b
$ hledger -f- print $ hledger -f- print
>2 /date is invalid/ >2 /is not a valid date/
>= 1 >= 1
# ** 5. dates must be followed by whitespace or newline # ** 5. dates must be followed by whitespace or newline