json object for FileNotFound

When a nonexistant file is passed to a command and  --json-error-messages
is enabled, output a JSON object indicating the problem.

(But git ls-files --error-unmatch still displays errors about such files in
some situations.)

I don't like the duplication of the name of the command introduced by this,
but I can't see a great way around it. One way would be to pass the Command
instead.

When json is not enabled, the stderr is unchanged. This is necessary
because some commands like find have custom output. So dislaying
"find foo not found" would be wrong. So had to complicate things with
toplevelFileProblem having different output with and without json.

When not using --json-error-messages but still using --json, it displays
the error to stderr, but does display a json object without the error. It
does have an errorid though. Unsure how useful that behavior is.

Sponsored-by: Dartmouth College's Datalad project
This commit is contained in:
Joey Hess 2023-04-25 19:26:20 -04:00
parent 91ba0cc7fd
commit be36e208c2
No known key found for this signature in database
GPG key ID: DB12DB0FF05F8F38
29 changed files with 97 additions and 43 deletions

View file

@ -27,6 +27,8 @@ module Messages (
showEndFail,
showEndResult,
endResult,
ErrorId(..),
toplevelFileProblem,
toplevelWarning,
warning,
earlyWarning,
@ -34,6 +36,7 @@ module Messages (
indent,
JSON.JSONChunk(..),
maybeShowJSON,
maybeShowJSON',
showFullJSON,
showCustom,
showHeader,
@ -197,8 +200,18 @@ endResult :: Bool -> S.ByteString
endResult True = "ok"
endResult False = "failed"
toplevelMsg :: StringContainingQuotedPath -> StringContainingQuotedPath
toplevelMsg = ("git-annex: " <>)
toplevelFileProblem :: Bool -> ErrorId -> StringContainingQuotedPath -> String -> RawFilePath -> Maybe Key -> SeekInput -> Annex ()
toplevelFileProblem makeway errorid msg action file mkey si = do
maybeShowJSON' $ JSON.start action (Just file) mkey si
maybeShowJSON' $ JSON.errorid errorid
warning' makeway id (toplevelMsg (QuotedPath file <> " " <> msg))
maybeShowJSON' $ JSON.end False
toplevelWarning :: Bool -> StringContainingQuotedPath -> Annex ()
toplevelWarning makeway s = warning' makeway id ("git-annex: " <> s)
toplevelWarning makeway s = warning' makeway id (toplevelMsg s)
warning :: StringContainingQuotedPath -> Annex ()
warning = warning' True indent
@ -207,10 +220,10 @@ earlyWarning :: StringContainingQuotedPath -> Annex ()
earlyWarning = warning' False id
warning' :: Bool -> (S.ByteString -> S.ByteString) -> StringContainingQuotedPath -> Annex ()
warning' makeway consolewhitespacef w = do
warning' makeway consolewhitespacef msg = do
when makeway $
outputMessage JSON.none id "\n"
outputError (\s -> consolewhitespacef s <> "\n") w
outputError (\s -> consolewhitespacef s <> "\n") msg
{- Not concurrent output safe. -}
warningIO :: String -> IO ()
@ -226,6 +239,9 @@ indent = S.intercalate "\n" . map (" " <>) . S8.lines
maybeShowJSON :: JSON.JSONChunk v -> Annex ()
maybeShowJSON v = void $ withMessageState $ bufferJSON (JSON.add v)
maybeShowJSON' :: JSON.JSONBuilder -> Annex ()
maybeShowJSON' v = void $ withMessageState $ bufferJSON v
{- Shows a complete JSON value, only when in json mode. -}
showFullJSON :: JSON.JSONChunk v -> Annex Bool
showFullJSON v = withMessageState $ bufferJSON (JSON.complete v)