shellmet icon indicating copy to clipboard operation
shellmet copied to clipboard

Add the `$??` operator to pattern match on the ExitCode

Open chshersh opened this issue 5 years ago • 2 comments

Similar to $? but should allow handling different exit statuses.

($??) :: IO a -> (ExitCode -> IO a) -> IO a

chshersh avatar Jul 07 '20 14:07 chshersh

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 avatar Mar 23 '21 14:03 vrom911

@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...

chshersh avatar Mar 23 '21 17:03 chshersh