Skip to content

Commit

Permalink
Added type synonym for error in mimeUnrender and ResultStream
Browse files Browse the repository at this point in the history
Swapped String error to type synonym

Resolves haskell-servant#999
  • Loading branch information
Jim Stanton committed Oct 15, 2018
1 parent 26b98f0 commit 37e2501
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
6 changes: 4 additions & 2 deletions servant/src/Servant/API/ContentTypes.hs
Original file line number Diff line number Diff line change
Expand Up @@ -215,18 +215,20 @@ instance TL.TypeError ('TL.Text "No instance for (), use NoContent instead.")
-- >>> type MyAPI = "path" :> ReqBody '[MyContentType] Int :> Get '[JSON] Int
--
class Accept ctype => MimeUnrender ctype a where
mimeUnrender :: Proxy ctype -> ByteString -> Either String a
mimeUnrender :: Proxy ctype -> ByteString -> Either BodyDecodingError a
mimeUnrender p = mimeUnrenderWithType p (contentType p)

-- | Variant which is given the actual 'M.MediaType' provided by the other party.
--
-- In the most cases you don't want to branch based on the 'M.MediaType'.
-- See <https://github.com/haskell-servant/servant/pull/552 pr552> for a motivating example.
mimeUnrenderWithType :: Proxy ctype -> M.MediaType -> ByteString -> Either String a
mimeUnrenderWithType :: Proxy ctype -> M.MediaType -> ByteString -> Either BodyDecodingError a
mimeUnrenderWithType p _ = mimeUnrender p

{-# MINIMAL mimeUnrender | mimeUnrenderWithType #-}

type BodyDecodingError = String

class AllCTUnrender (list :: [*]) a where
canHandleCTypeH
:: Proxy list
Expand Down
4 changes: 3 additions & 1 deletion servant/src/Servant/API/Stream.hs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,9 @@ instance ToStreamGenerator [a] a where
-- @ResultStream@ that captures the setup, takedown, and incremental logic for
-- a read, being an IO continuation that takes a producer of Just either values
-- or errors that terminates with a Nothing.
newtype ResultStream a = ResultStream { runResultStream :: forall b. (IO (Maybe (Either String a)) -> IO b) -> IO b }
newtype ResultStream a = ResultStream { runResultStream :: forall b. (IO (Maybe (Either BodyDecodingError a)) -> IO b) -> IO b }

type BodyDecodingError = String

-- | FromResultStream is intended to be implemented for types such as Conduit, Pipe, etc. By implementing this class, all such streaming abstractions can be used directly on the client side for talking to streaming endpoints.
class FromResultStream a b | b -> a where
Expand Down

0 comments on commit 37e2501

Please sign in to comment.