diff --git a/ChangeLog.md b/ChangeLog.md index 95a6483..551a20d 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -1,5 +1,9 @@ # ChangeLog for typed-process +* Format stdout and stderr in `ExitCodeException` assuming they are in + UTF-8. See [#87](https://github.com/fpco/typed-process/pull/87). + Thanks to @9999years for the legwork on this change. + ## 0.2.12.0 * Add `getPid`, `exitCodeExceptionWithOutput`, diff --git a/package.yaml b/package.yaml index 0339e8a..311cafa 100644 --- a/package.yaml +++ b/package.yaml @@ -19,6 +19,7 @@ dependencies: - bytestring - process >=1.2 - stm +- text - transformers - unliftio-core diff --git a/src/System/Process/Typed/Internal.hs b/src/System/Process/Typed/Internal.hs index 18942e1..a5ee0d1 100644 --- a/src/System/Process/Typed/Internal.hs +++ b/src/System/Process/Typed/Internal.hs @@ -23,9 +23,11 @@ import Control.Concurrent.Async (async) import Control.Concurrent.STM (newEmptyTMVarIO, atomically, putTMVar, readTMVar, STM, tryPutTMVar, throwSTM) import System.Exit (ExitCode) import qualified Data.ByteString.Lazy as L -import qualified Data.ByteString.Lazy.Char8 as L8 import Data.String (IsString (fromString)) import Control.Monad.IO.Unlift +import qualified Data.Text.Encoding.Error as TEE +import qualified Data.Text.Lazy as TL +import qualified Data.Text.Lazy.Encoding as TLE #if MIN_VERSION_process(1, 4, 0) && !WINDOWS import System.Posix.Types (GroupID, UserID) @@ -620,11 +622,15 @@ instance Show ExitCodeException where , show (eceProcessConfig ece) { pcEnv = Nothing } , if L.null (eceStdout ece) then "" - else "Standard output:\n\n" ++ L8.unpack (eceStdout ece) + else "Standard output:\n\n" ++ unpack (eceStdout ece) , if L.null (eceStderr ece) then "" - else "Standard error:\n\n" ++ L8.unpack (eceStderr ece) + else "Standard error:\n\n" ++ unpack (eceStderr ece) ] + where + -- Format with UTF-8, because we have to choose some encoding, + -- and UTF-8 is the least likely to be wrong in general. + unpack = TL.unpack . TLE.decodeUtf8With TEE.lenientDecode -- | Wrapper for when an exception is thrown when reading from a child -- process, used by 'byteStringOutput'.