Skip to content

Commit

Permalink
Handle onError event in request body stream
Browse files Browse the repository at this point in the history
  • Loading branch information
owickstrom committed May 18, 2017
1 parent 77a19fb commit 60bde7a
Showing 1 changed file with 20 additions and 7 deletions.
27 changes: 20 additions & 7 deletions src/Hyper/Node/Server.purs
Original file line number Diff line number Diff line change
Expand Up @@ -84,23 +84,36 @@ instance bufferNodeResponse :: (MonadAff e m)
toResponse buf =
ipure (write buf)

-- Helper function that reads a Stream into a Buffer, and throws error
-- in `Aff` when failed.
readBodyAsBuffer
:: forall e.
HttpRequest
-> Aff (http :: HTTP, avar :: AVAR, buffer :: BUFFER | e) Buffer
readBodyAsBuffer (HttpRequest request _) = do
let stream = HTTP.requestAsStream request
completeBody <- makeVar
bodyResult <- makeVar
chunks <- makeVar' []
res <- liftEff $
catchException (pure <<< Left) (Right <$> fillBody stream chunks completeBody)
either throwError (const (takeVar completeBody)) res
fillResult <- liftEff $
catchException (pure <<< Left) (Right <$> fillBody stream chunks bodyResult)
-- Await the body, or an error.
body <- takeVar bodyResult
-- Return the body, if neither `fillResult` nor `body` is a `Left`.
either throwError pure (fillResult *> body)
where
fillBody stream chunks completeBody = do
fillBody stream chunks bodyResult = do
-- Append all chunks to the body buffer.
Stream.onData stream \chunk ->
void (launchAff (modifyVar (_ <> [chunk]) chunks))
Stream.onEnd stream $
void (launchAff (takeVar chunks >>= concat' >>= putVar completeBody))
-- Complete with `Left` on error.
Stream.onError stream $
void <<< launchAff <<< putVar bodyResult <<< Left
-- Complete with `Right` on successful "end" event.
Stream.onEnd stream $ void $ launchAff $
takeVar chunks
>>= concat'
>>= (pure <<< Right)
>>= putVar bodyResult
concat' = liftEff <<< Buffer.concat

instance readableBodyHttpRequestString :: (Monad m, MonadAff (http :: HTTP, avar :: AVAR, buffer :: BUFFER | e) m)
Expand Down

0 comments on commit 60bde7a

Please sign in to comment.