shellmet
shellmet copied to clipboard
Add the `$??` operator to pattern match on the ExitCode
Similar to $? but should allow handling different exit statuses.
($??) :: IO a -> (ExitCode -> IO a) -> IO a
I am trying to implement that but can't actually catch on the ExitCall as we are using callCommand in the main instance.
Here is my implementation:
infix 4 $??
($??) :: IO a -> (ExitCode -> IO a) -> IO a
action $?? catchCode = action `catch` catchCode
{-# INLINE ($??) #-}
And when I try to test it:
λ: :{
λ| safeExit :: ExitCode -> IO ()
λ| safeExit ExitSuccess = putStrLn "This is Safe Exit Success"
λ| safeExit (ExitFailure n) = putStrLn $ "This is Safe Exit Failure" <> show n
λ| :}
λ:
λ: "exit" ["1"] $?? safeExit
⚙ exit 1
*** Exception: callCommand: exit 1 (exit 1): failed
Do you have any ideas on how to overcome this, @chshersh ? 🙏🏼
@vrom911 Oh, dear, that's unfortunate 😞 I have no idea how to overcome this.
I see that the callCommand uses the withCreateProcess_ function and rethrows the ExitCode as IOError. So, probably we can change some internals to take this into consideration (like, rethrow the ExitCode itself), but this may break a function like $?. So, not sure about the best way to resolve this problem...