-- Hoogle documentation, generated by Haddock -- See Hoogle, http://www.haskell.org/hoogle/ -- | Defines generic 'Envelope' type to wrap reponses from a JSON API. -- -- Please see README.md @package envelope @version 0.2.2.0 -- | This module contains the Envelope type and helper functions. -- The Envelope type can be used to wrap responses from a JSON -- API. -- -- The following is an example of using this package. -- -- First, we will import some needed modules. -- --
-- >>> import qualified Data.ByteString.Lazy.Char8 as C8 -- -- >>> import Data.Aeson (decode, encode) ---- -- Let's look at how a success reponse is encoded and decoded. It is -- encoded as an object with a single member: "data". -- --
-- >>> let successEnvelope = toSuccessEnvelope 3 :: Envelope Text Int
--
-- >>> C8.putStrLn $ encode successEnvelope
-- {"data":3}
--
-- >>> decode "{\"data\":3}" :: Maybe (Envelope Text Int)
-- Just (EnvelopeSuccess (Success 3))
--
--
-- Now lets look at how an error response is encoded and decoded. It is
-- encoded as an object with two members: "extra" and
-- "error".
--
--
-- >>> let errorEnvelope = toErrEnvelope "DB_ERROR" "there was an error with the database" :: Envelope String Int
--
-- >>> C8.putStrLn $ encode errorEnvelope
-- {"extra":"there was an error with the database","error":"DB_ERROR"}
--
-- >>> decode "{\"extra\":\"there was an error with the database\",\"error\":\"DB_ERROR\"}" :: Maybe (Envelope String Int)
-- Just (EnvelopeErr (Err {errErr = "DB_ERROR", errExtra = Just "there was an error with the database"}))
--
--
-- The Success and Err types are used within the
-- Envelope type synonym.
module Web.Envelope
-- | Main type to be used. Wrapper around responses from an API, mainly
-- used with a JSON API.
--
-- Type synonym around Envelope'.
type Envelope e a = Envelope' (Err e) (Success a)
-- | Wrapper around either a success or an error. Isomorphic to
-- Either.
--
-- The only interesting part of this type is the ToJSON and
-- FromJSON instances.
data Envelope' e a
EnvelopeErr :: e -> Envelope' e a
EnvelopeSuccess :: a -> Envelope' e a
-- | Newtype wrapper to be able to provide specific instances. Used with
-- Envelope.
newtype Success a
Success :: a -> Success a
-- | Wrapper to add an extra field with info about the error. Used with
-- Envelope.
data Err e
Err :: e -> Maybe Text -> Err e
-- | Actual error information we want to send.
[errErr] :: Err e -> e
-- | Additional error information in plain text.
[errExtra] :: Err e -> Maybe Text
-- | Smart constructor for Err.
--
--
-- >>> toErr "DB_ERROR" "an error occurred"
-- Err {errErr = "DB_ERROR", errExtra = Just "an error occurred"}
--
toErr :: e -> Text -> Err e
-- | Smart constructor for Err that doesn't use an errExtra.
--
--
-- >>> toErr' "DB_ERROR"
-- Err {errErr = "DB_ERROR", errExtra = Nothing}
--
toErr' :: e -> Err e
-- | Throw an 'Err e' using throwError in a Monad
-- that implements MonadError.
--
-- If you have ExceptT (Err e) somewhere inside
-- your monad transformer stacks, this function can be used to throw an
-- error (return early) in a function.
--
--
-- >>> import Control.Monad.Except (runExcept)
--
-- >>> throwEnvelopeErr "BAD_ERROR" "a very bad error occurred!" :: Either (Err String) Int
-- Left (Err {errErr = "BAD_ERROR", errExtra = Just "a very bad error occurred!"})
--
--
-- Here is a longer example using a monad stack.
--
-- -- type MyMonadStack = ReaderT Int (ExceptT (Err String) IO) -- -- doSomething :: Int -> MyMonadStack Int -- doSomething int = -- if int < 0 -- then -- throwEnvelopeErr "INT_TOO_SMALL" "the integer passed to doSomething is too small" -- else -- return (int + 1) --throwEnvelopeErr :: (MonadError (Err e) m) => e -> Text -> m a -- | Like throwEnvelopeErr but without providing a message. -- --
-- >>> import Control.Monad.Except (runExcept)
--
-- >>> throwEnvelopeErr "BAD_ERROR" "a very bad error occurred!" :: Either (Err String) Int
-- Left (Err {errErr = "BAD_ERROR", errExtra = Just "a very bad error occurred!"})
--
throwEnvelopeErr' :: (MonadError (Err e) m) => e -> m a
-- | Wrap an a in a success Envelope.
--
-- -- >>> toSuccessEnvelope 3 :: Envelope String Int -- EnvelopeSuccess (Success 3) --toSuccessEnvelope :: a -> Envelope e a -- | Wrap an a and an additional message in an error -- Envelope. -- --
-- >>> toErrEnvelope "DB_ERROR" "there was an error with the database" :: Envelope String Int
-- EnvelopeErr (Err {errErr = "DB_ERROR", errExtra = Just "there was an error with the database"})
--
toErrEnvelope :: e -> Text -> Envelope e a
-- | Wrap an a in an error Envelope.
--
--
-- >>> toErrEnvelope' "DB_ERROR" :: Envelope String Int
-- EnvelopeErr (Err {errErr = "DB_ERROR", errExtra = Nothing})
--
toErrEnvelope' :: e -> Envelope e a
instance GHC.Show.Show e => GHC.Show.Show (Web.Envelope.Err e)
instance GHC.Generics.Generic (Web.Envelope.Err e)
instance GHC.Classes.Eq e => GHC.Classes.Eq (Web.Envelope.Err e)
instance Data.Data.Data e => Data.Data.Data (Web.Envelope.Err e)
instance GHC.Show.Show a => GHC.Show.Show (Web.Envelope.Success a)
instance GHC.Generics.Generic (Web.Envelope.Success a)
instance GHC.Classes.Eq a => GHC.Classes.Eq (Web.Envelope.Success a)
instance Data.Data.Data a => Data.Data.Data (Web.Envelope.Success a)
instance (GHC.Show.Show a, GHC.Show.Show e) => GHC.Show.Show (Web.Envelope.Envelope' e a)
instance GHC.Generics.Generic (Web.Envelope.Envelope' e a)
instance (GHC.Classes.Eq a, GHC.Classes.Eq e) => GHC.Classes.Eq (Web.Envelope.Envelope' e a)
instance (Data.Data.Data a, Data.Data.Data e) => Data.Data.Data (Web.Envelope.Envelope' e a)
instance (Data.Aeson.Types.ToJSON.ToJSON e, Data.Aeson.Types.ToJSON.ToJSON a) => Data.Aeson.Types.ToJSON.ToJSON (Web.Envelope.Envelope' e a)
instance (Data.Aeson.Types.FromJSON.FromJSON e, Data.Aeson.Types.FromJSON.FromJSON a) => Data.Aeson.Types.FromJSON.FromJSON (Web.Envelope.Envelope' e a)
instance (Web.Internal.FormUrlEncoded.ToForm a, Web.Internal.FormUrlEncoded.ToForm e) => Web.Internal.FormUrlEncoded.ToForm (Web.Envelope.Envelope' e a)
instance (Web.Internal.FormUrlEncoded.FromForm a, Web.Internal.HttpApiData.FromHttpApiData e) => Web.Internal.FormUrlEncoded.FromForm (Web.Envelope.Envelope' (Web.Envelope.Err e) a)
instance Data.Aeson.Types.ToJSON.ToJSON a => Data.Aeson.Types.ToJSON.ToJSON (Web.Envelope.Success a)
instance Data.Aeson.Types.FromJSON.FromJSON e => Data.Aeson.Types.FromJSON.FromJSON (Web.Envelope.Success e)
instance Web.Internal.FormUrlEncoded.ToForm a => Web.Internal.FormUrlEncoded.ToForm (Web.Envelope.Success a)
instance Web.Internal.FormUrlEncoded.FromForm a => Web.Internal.FormUrlEncoded.FromForm (Web.Envelope.Success a)
instance Data.Aeson.Types.ToJSON.ToJSON e => Data.Aeson.Types.ToJSON.ToJSON (Web.Envelope.Err e)
instance Data.Aeson.Types.FromJSON.FromJSON e => Data.Aeson.Types.FromJSON.FromJSON (Web.Envelope.Err e)
instance Web.Internal.HttpApiData.ToHttpApiData e => Web.Internal.FormUrlEncoded.ToForm (Web.Envelope.Err e)
instance Web.Internal.HttpApiData.FromHttpApiData e => Web.Internal.FormUrlEncoded.FromForm (Web.Envelope.Err e)