feat(users/Profpatsch/whatcd-resolver): show json val on parse err

The json parsing library gives us an error path where the parse
failed, which means we can index into the path to show the json value
that failed us.

This can be quite expensive (and large!) of course, but the error
message clarity is worth it methinks.

Change-Id: Icacbd799254aaecd4a939ca13e6070d68a78138d
Reviewed-on: https://cl.tvl.fyi/c/depot/+/12952
Tested-by: BuildkiteCI
Reviewed-by: Profpatsch <mail@profpatsch.de>
This commit is contained in:
Profpatsch 2025-01-04 21:45:23 +01:00
parent 0319b5e6c0
commit 722499d8a9
5 changed files with 94 additions and 7 deletions

View file

@ -19,6 +19,7 @@ module Http
where
import AppT
import Data.Aeson qualified as Json
import Data.Aeson.BetterErrors qualified as Json
import Data.CaseInsensitive (CI (original))
import Data.Char qualified as Char
@ -93,12 +94,18 @@ httpJson opts parser req = inSpan' "HTTP Request (JSON)" $ \span -> do
Left [fmt|Server returned a body with unspecified content type|]
| code <- statusCode -> Left $ AppExceptionPretty [[fmt|Server returned an non-200 error code, code {code}:|], pretty resp]
)
>>= assertM
span
( \body ->
Json.parseStrict parser body
& first (AppExceptionTree . Json.parseErrorTree "could not parse HTTP response")
)
>>= \body -> do
val <-
Json.eitherDecodeStrict body
& first (\err -> AppExceptionTree $ nestedError "HTTP response was not valid JSON" (err & stringToText & newError & singleError))
& orAppThrow span
let res = Json.parseValue parser val
case res of
Left e -> do
let prettyErr = Json.parseErrorTreeValCtx "could not parse HTTP response" val e
appThrow span (AppExceptionTree prettyErr)
Right a -> pure a
doRequestJson ::
(MonadOtel m) =>