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

Integrate RIO monad part 1 #1033

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

Jimbo4350
Copy link
Contributor

@Jimbo4350 Jimbo4350 commented Jan 28, 2025

Changelog

- description: |
    Integrate RIO monad in "compatible signed-transaction" command 
  type:
  - refactoring    # QoL changes

Context

How to trust this PR

Highlight important bits of the PR that will make the review faster. If there are commands the reviewer can run to observe the new behavior, describe them.

Checklist

  • Commit sequence broadly makes sense and commits have useful messages
  • New tests are added if needed and existing tests are updated. See Running tests for more details
  • Self-reviewed the diff

@Jimbo4350 Jimbo4350 force-pushed the jordan/integrate-RIO-monad-part-1 branch 4 times, most recently from 69744e1 to 989ba8a Compare January 28, 2025 13:35
@Jimbo4350 Jimbo4350 marked this pull request as ready for review January 28, 2025 13:36
Copy link
Contributor

@carbolymer carbolymer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we finish ADR first?

@Jimbo4350 Jimbo4350 force-pushed the jordan/integrate-RIO-monad-part-1 branch 6 times, most recently from 510a872 to 796aaf1 Compare February 10, 2025 19:14
@Jimbo4350 Jimbo4350 requested a review from carbolymer February 10, 2025 19:17
@Jimbo4350 Jimbo4350 force-pushed the jordan/integrate-RIO-monad-part-1 branch from 796aaf1 to 5a36d5d Compare February 11, 2025 15:23
Copy link
Contributor

@carbolymer carbolymer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tactical merge block - I need to play with it

Comment on lines +45 to +49
fromEitherIOCli action = do
result <- liftIO action
case result of
Left e -> throwCliError $ CustomCliException e callStack
Right a -> return a
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
fromEitherIOCli action = do
result <- liftIO action
case result of
Left e -> throwCliError $ CustomCliException e callStack
Right a -> return a
fromEitherIOCli action = liftIO action >>= fromEitherCli


-- | This is a temporary function until all commands are migrated to RIO
-- Once this happens we can remove this function and rely on `toplevelExceptionHandler`
executeRio :: RIO () () -> IO ()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about an alias type CIO e a = HasCallStack => RIO e a ?

-> ExceptT CompatibleTransactionError IO ()
. HasCallStack
=> CompatibleTransactionCmds era
-> RIO () ()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why concrete environment?

Suggested change
-> RIO () ()
-> RIO e ()

executeRio :: RIO () () -> IO ()
executeRio r = do
runRIO () r
`catch` (\(e :: SomeException) -> putStrLn $ displayException e)
Copy link
Contributor

@carbolymer carbolymer Feb 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
`catch` (\(e :: SomeException) -> putStrLn $ displayException e)
`catch` (\(e :: SomeException) -> hPutStr stderr $ displayException e)

you'd need some imports for that

  1. The exitcode for cardano-cli here is 0 which is wrong.

Copy link
Contributor

@carbolymer carbolymer Feb 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can remove this handler and just let Cardano.CLI.TopHandler.toplevelExceptionHandler do the job.
I'd just simplify Cardano.CLI.TopHandler.renderSomeException to just use displayException. I think show there is just making output more noisy without any useful info.

throwCliError = throwIO

fromEitherCli :: (HasCallStack, MonadIO m, Show e, Typeable e, Error e) => Either e a -> m a
fromEitherCli = \case
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
fromEitherCli = \case
fromEitherCli = withFrozenCallStack $ \case

]

validatedRefInputs <-
fromEitherCli . first CompatibleTxCmdError . validateTxInsReference $
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this extra wrapping needed?

Suggested change
fromEitherCli . first CompatibleTxCmdError . validateTxInsReference $
fromEitherCli . validateTxInsReference $

@@ -14,6 +14,9 @@ data BootstrapWitnessError
MissingNetworkIdOrByronAddressError
deriving Show

instance Error BootstrapWitnessError where
prettyError = renderBootstrapWitnessError
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's remove render* functions for Errors and just use prettyError. Could be out of scope for this PR, because it's only slightly related.

data CustomCliException where
CustomCliException
:: (Show error, Typeable error, Error error)
=> error -> CallStack -> CustomCliException
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why explicit CallStack argument? The way it's used it can be an implicit one.

{-# LANGUAGE GADTs #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE UndecidableInstances #-}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
{-# LANGUAGE UndecidableInstances #-}


fromEitherIOCli :: (HasCallStack, MonadIO m, Show e, Typeable e, Error e) => IO (Either e a) -> m a
fromEitherIOCli action = do
result <- liftIO action
Copy link
Contributor

@carbolymer carbolymer Feb 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is more complicated than just liftIO. We need to catch all synchronous exceptions here and wrap them, and also provide the call stack. So basically we need analogous wrapper to CustomCliException but for Exceptions this time.

This is because IOException for example does not carry the call stack with it (yet), so we should manually add it to it.

Unfortunately it'll be just a call stack up to fromEitherIOCli call.


module Cardano.CLI.Compatible.Exception
( CustomCliException (..)
, throwCliError
Copy link
Contributor

@carbolymer carbolymer Feb 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this should be part of the API of this module. This function isn't useful on its own - you still need to attach call stack manually.

Suggested change
, throwCliError

Copy link
Contributor

@carbolymer carbolymer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks promising. We're getting there. Can you address my comments?

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

Successfully merging this pull request may close these issues.

2 participants