--json-exceptions

Added a --json-exceptions option, which makes some exceptions be output in json.

The distinction is that --json-error-messages is for messages relating
to a particular ActionItem, while --json-exceptions is for messages that
are not, eg ones for a file that does not exist.

It's unfortunate that we need two switches with such a fine distinction
between them, but I'm worried about maintaining backwards compatability
in the json output, to avoid breaking anything that parses it, and this was
the way to make sure I didn't.

toplevelWarning is generally used for the latter kind of message. And
the other calls to toplevelWarning could be converted to showException. The
only possible gotcha is that if toplevelWarning is ever called after
starting acting on a file, it will add to the --json-error-messages of the
json displayed for that file and converting to showException would be a
behavior change. That seems unlikely, but I didn't convery everything to
avoid needing to satisfy myself it was not a concern.

Sponsored-by: Dartmouth College's Datalad project
This commit is contained in:
Joey Hess 2023-04-25 17:02:25 -04:00
parent a474c9c63b
commit a325524454
No known key found for this signature in database
GPG key ID: DB12DB0FF05F8F38
32 changed files with 304 additions and 81 deletions

View file

@ -1,6 +1,6 @@
{- git-annex command-line JSON output and input
-
- Copyright 2011-2021 Joey Hess <id@joeyh.name>
- Copyright 2011-2023 Joey Hess <id@joeyh.name>
-
- Licensed under the GNU AGPL version 3 or higher.
-}
@ -27,6 +27,7 @@ module Messages.JSON (
ObjectMap(..),
JSONActionItem(..),
AddJSONActionItemFields(..),
exceptionObject,
) where
import Control.Applicative
@ -218,3 +219,14 @@ newtype AddJSONActionItemFields a = AddJSONActionItemFields a
instance ToJSON' a => ToJSON' (AddJSONActionItemFields a) where
toJSON' (AddJSONActionItemFields a) = object [ ("fields", toJSON' a) ]
exceptionObject :: String -> String -> Maybe RawFilePath -> Object
exceptionObject eid msg mfile = case o of
Object o' -> o'
_ -> error "internal"
where
o = object
[ "exception" .= toJSON' eid
, "message" .= toJSON' msg
, "file" .= toJSON' (maybe "" decodeBS mfile)
]