@@ -229,7 +229,7 @@ import Data.Text.Internal.Lazy.Fusion (stream, unstream)
229229import Data.Text.Internal.Lazy (Text(..), chunk, empty, foldlChunks,
230230 foldrChunks, smallChunkSize)
231231import Data.Text.Internal (firstf, safe, text)
232−import Data.Text.Lazy.Encoding (decodeUtf8, encodeUtf8)
232+import Data.Text.Lazy.Encoding (decodeUtf8', encodeUtf8)
233233import qualified Data.Text.Internal.Functions as F
234234import Data.Text.Internal.Lazy.Search (indices)
235235#if __GLASGOW_HASKELL__ >= 702
@@ -376,7 +376,11 @@ instance NFData Text where
376376
377377instance Binary Text where
378378 put t = put (encodeUtf8 t)
379− get = P.fmap decodeUtf8 get
379+ get = do
380+ bs <- get
381+ case decodeUtf8' bs of
382+ P.Left exn -> P.fail (P.show exn)
383+ P.Right a -> P.return a
380384
381385-- | This instance preserves data abstraction at the cost of inefficiency.
382386-- We omit reflection services for the sake of data abstraction.