f-o-a-m / purescript-web3

a purescript library for the web3 api
Apache License 2.0
127 stars 24 forks source link

change type of Web3 to `Web3 (ReaderT Provider (ExceptT Web3Error Aff) a)` #156

Closed srghma closed 1 year ago

srghma commented 2 years ago

Web3 a = Provider -> Aff (Either Web3 a)

will resolve

-- | Run an asynchronous `ETH` action
runWeb3 :: forall a. Provider -> Web3 a -> Aff (Either Web3Error a)
runWeb3 p (Web3 action) =
  attempt (runReaderT action p)
    >>= case _ of
        Left err -> maybe (throwError err) (pure <<< Left) $ parseMsg $ message err
        Right x -> pure $ Right x
  where
  -- NOTE: it's a bit hacky
  -- for this to work, errors of type `Web3Error` should be converted to json
  -- using `genericEncodeJSON defaultOptions` and then Error
  -- should be created with json string as a message.
  -- see Network.Ethereum.Web3.JsonRPC#asError
  parseMsg :: String -> Maybe Web3Error
  parseMsg msg = hush $ runExcept $ genericDecodeJSON defaultOptions msg

which is used to catch errors sent from

instance remoteBase :: (Decode a) => Remote (Web3 a) where
srghma commented 2 years ago

and also

data Web3Error
  = Rpc RpcError
  | RemoteError String -- better be encoded as original type Error
  | ParserError String -- Too
  | NullError