Hello Everyone,
I have the following code that throws an exception in the event the json parsing fails:
tryObsFromResponse :: (MonadLogger m, MonadCatch m) => LB.ByteString -> m ObsETL
tryObsFromResponse res = do
-- Json ByteString -> Maybe ObsEtlInput
obsInput :: ObsEtlInput <- case decode res of
Just obs -> pure obs
Nothing -> do
App.logErrorN "Failed to decode the json"
App.throw
$ App.ValueException
(Just "\nThe obsetl data decoding failed")
tryBuildObs <- fromInputObsEtl obsInput -- :: m (Either ObsException ObsETL)
case tryBuildObs of -- :: Either ObsException ObsETL
Left e -> App.throw e
Right obsEtl -> pure obsEtl
It is called by way of the following:
api :: ProjectId -> GQLRequest -> AppObs GQLResponse
api pid req = do
_ <- setDbWithS3 pid
interpreter gqlRoot req
Finally, AppObs
is a custom monad used in servant
which includes Control.Exception.Safe
where throw
is defined.
How should I think about getting the exception into the response generated by servant?