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

@ -20,6 +20,7 @@ module Messages.JSON (
addErrorMessage,
note,
info,
errorid,
add,
complete,
progress,
@ -51,6 +52,7 @@ import Utility.Metered
import Utility.Percentage
import Utility.Aeson
import Utility.FileSystemEncoding
import Types.Messages
-- A global lock to avoid concurrent threads emitting json at the same time.
{-# NOINLINE emitLock #-}
@ -68,7 +70,8 @@ emit' b = do
putMVar emitLock ()
-- Building up a JSON object can be done by first using start,
-- then add and note any number of times, and finally complete.
-- then add and note and errorid any number of times, and finally
-- complete.
type JSONBuilder = Maybe (Object, Bool) -> Maybe (Object, Bool)
none :: JSONBuilder
@ -112,6 +115,12 @@ note s (Just (o, e)) = Just (HM.unionWith combinelines (HM.singleton "note" (toJ
String (old <> "\n" <> new)
combinelines new _old = new
errorid :: ErrorId -> JSONBuilder
errorid _ Nothing = Nothing
errorid eid (Just (o, e)) = Just (HM.unionWith replaceold (HM.singleton "errorid" (toJSON' (show eid))) o, e)
where
replaceold new _old = new
info :: String -> JSONBuilder
info s _ = case j of
Object o -> Just (o, True)