@@ -80,7 +80,7 @@ We have three kinds of streams: readable streams, writable streams, and transfor
8080import Array exposing (Array)
8181import Basics exposing (Bool, Int, (<|), (|>), (++), max)
8282import Bytes exposing (Bytes)
83- import Maybe exposing (Maybe)
83+ import Maybe exposing (Maybe(..) )
8484import Result exposing (Result(..))
8585import String exposing (String)
8686import Task exposing (Task)
@@ -160,12 +160,26 @@ read =
160160 Gren.Kernel.Stream.read
161161
162162
163- {-| Reads `Bytes` off the stream and attempt to convert it into `String`.
163+ {-| Reads `Bytes` off the stream and attempt to convert it into `String`. If the conversion fails,
164+ the stream will be cancelled.
164165-}
165- readBytesAsString : Readable Bytes -> Task Error (Maybe String)
166+ readBytesAsString : Readable Bytes -> Task Error String
166167readBytesAsString stream =
167168 read stream
168- |> Task.map Bytes.toString
169+ |> Task.andThen
170+ (\bytes ->
171+ when Bytes.toString bytes is
172+ Just str ->
173+ Task.succeed str
174+
175+ Nothing ->
176+ let
177+ reason =
178+ "Failed to convert bytes to string"
179+ in
180+ cancelReadable reason stream
181+ |> Task.andThen (\_ -> Task.fail (Cancelled reason))
182+ )
169183
170184
171185{-| Reads values of the stream, incrementally building a value with the provided function, until
0 commit comments