Skip to content

Commit c1f6970

Browse files
committed
improve getJsonBody error output
1 parent 968ebbd commit c1f6970

File tree

4 files changed

+31
-11
lines changed

4 files changed

+31
-11
lines changed

freckle-app/CHANGELOG.md

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
## [_Unreleased_](https://github.com/freckle/freckle-app/compare/freckle-app-v1.20.1.1...main)
1+
## [_Unreleased_](https://github.com/freckle/freckle-app/compare/freckle-app-v1.20.1.2...main)
2+
3+
## [v1.20.1.1](https://github.com/freckle/freckle-app/compare/freckle-app-v1.20.1.1...freckle-app-v1.20.1.2)
4+
5+
Improve quality of error message output from `getJsonBody` and `getCsvBody`
6+
in `Freckle.App.Test.Yesod`.
27

38
## [v1.20.1.1](https://github.com/freckle/freckle-app/compare/freckle-app-v1.20.1.0...freckle-app-v1.20.1.1)
49

freckle-app/freckle-app.cabal

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ cabal-version: 1.22
55
-- see: https://github.com/sol/hpack
66

77
name: freckle-app
8-
version: 1.20.1.1
8+
version: 1.20.1.2
99
synopsis: Haskell application toolkit used at Freckle
1010
description: Please see README.md
1111
category: Utils

freckle-app/library/Freckle/App/Test/Yesod.hs

+23-8
Original file line numberDiff line numberDiff line change
@@ -79,19 +79,20 @@ import Control.Monad.State (StateT)
7979
import Control.Monad.Trans.Maybe (MaybeT)
8080
import Control.Monad.Trans.Resource (ResourceT)
8181
import Control.Monad.Validate (ValidateT)
82-
import Data.Aeson (FromJSON, eitherDecode)
82+
import Data.Aeson (FromJSON)
8383
import Data.BCP47 (BCP47)
8484
import Data.BCP47 qualified as BCP47
8585
import Data.ByteString (ByteString)
8686
import Data.ByteString qualified as BS
8787
import Data.ByteString.Lazy qualified as BSL
8888
import Data.CaseInsensitive (CI)
89-
import Data.Csv (FromNamedRecord, decodeByName)
89+
import Data.Csv qualified as CSV
9090
import Data.Text qualified as T
9191
import Data.Vector qualified as V
9292
import Freckle.App.Test (expectationFailure)
9393
import Network.HTTP.Types.Header (hAccept, hAcceptLanguage, hContentType)
9494
import Network.Wai.Test (SResponse (..))
95+
import Test.HUnit qualified as HUnit
9596
import Web.Cookie (SetCookie)
9697
import Yesod.Core (RedirectUrl, Yesod)
9798
import Yesod.Test
@@ -111,6 +112,7 @@ import Yesod.Test
111112
, withResponse
112113
)
113114
import Yesod.Test qualified
115+
import Yesod.Test.Internal (getBodyTextPreview)
114116

115117
class (MonadIO m, Yesod site) => MonadYesodExample site m | m -> site where
116118
liftYesodExample :: YesodExample site a -> m a
@@ -161,18 +163,31 @@ testSetCookie = liftYesodExample . Yesod.Test.testSetCookie
161163
-- | Get the body of the most recent response and decode it as JSON
162164
getJsonBody
163165
:: forall a m site. (MonadYesodExample site m, FromJSON a, HasCallStack) => m a
164-
getJsonBody = either err pure . eitherDecode =<< getRawBody
165-
where
166-
err e = expectationFailure $ "Error decoding JSON response body: " <> e
166+
getJsonBody = liftYesodExample Yesod.Test.requireJSONResponse
167167

168168
-- | Get the body of the most recent response and decode it as CSV
169169
getCsvBody
170170
:: forall a m site
171-
. (MonadYesodExample site m, FromNamedRecord a, HasCallStack)
171+
. (MonadYesodExample site m, CSV.FromNamedRecord a, HasCallStack)
172172
=> m [a]
173-
getCsvBody = either err (pure . V.toList . snd) . decodeByName =<< getRawBody
173+
getCsvBody =
174+
liftYesodExample $
175+
withResponse $ \(SResponse _status _headers body) ->
176+
-- todo - check the response header first
177+
case fmap (V.toList . snd) (CSV.decodeByName body) of
178+
Left err ->
179+
failure $
180+
T.concat
181+
[ "Failed to parse CSV response; error: "
182+
, T.pack err
183+
, "CSV: "
184+
, getBodyTextPreview body
185+
]
186+
Right v -> pure v
174187
where
175-
err e = expectationFailure $ "Error decoding CSV response body: " <> e
188+
failure reason = do
189+
liftIO $ HUnit.assertFailure $ T.unpack reason
190+
error ""
176191

177192
-- | Get the body of the most recent response as a byte string
178193
getRawBody

freckle-app/package.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: freckle-app
2-
version: 1.20.1.1
2+
version: 1.20.1.2
33
maintainer: Freckle Education
44
category: Utils
55
github: freckle/freckle-app

0 commit comments

Comments
 (0)