Print preview of JSON body in case of parse failure

This commit is contained in:
Maximilian Tagher 2019-11-28 22:32:42 -05:00
parent 91b75741dd
commit 0025226af6
2 changed files with 8 additions and 2 deletions

View File

@ -218,3 +218,4 @@ acceptsJson = (maybe False ((== "application/json") . B8.takeWhile (/= ';'))
. listToMaybe
. reqAccept)
`liftM` getRequest

View File

@ -624,8 +624,13 @@ requireJSONResponse = do
isJSONContentType
(failure $ T.pack $ "Expected `Content-Type: application/json` in the headers, got: " ++ show headers)
case eitherDecode' body of
-- TODO: include full body in error message?
Left err -> failure $ T.concat ["Failed to parse JSON response; error: ", T.pack err]
Left err -> do
let characterLimit = 1024
textBody = TL.toStrict $ decodeUtf8 body
bodyPreview = if T.length textBody < characterLimit
then textBody
else T.take characterLimit textBody <> "... (use `printBody` to see complete response body)"
failure $ T.concat ["Failed to parse JSON response; error: ", T.pack err, "JSON: ", bodyPreview]
Right v -> return v
-- | Outputs the last response body to stderr (So it doesn't get captured by HSpec)