Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the $?? operator to pattern match on the ExitCode #21

Open
chshersh opened this issue Jul 7, 2020 · 2 comments
Open

Add the $?? operator to pattern match on the ExitCode #21

chshersh opened this issue Jul 7, 2020 · 2 comments
Assignees
Labels
enhancement New feature or request

Comments

@chshersh
Copy link
Contributor

chshersh commented Jul 7, 2020

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

($??) :: IO a -> (ExitCode -> IO a) -> IO a
@chshersh chshersh added the enhancement New feature or request label Jul 7, 2020
@vrom911 vrom911 self-assigned this Mar 23, 2021
@vrom911
Copy link
Member

vrom911 commented Mar 23, 2021

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 ? 🙏🏼

@chshersh
Copy link
Contributor Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants