简体   繁体   中英

throwError in Halogen component queries

I'm struggling with achieving following goal: I have the API requests typed in a manner that they return either a desired value, or an error when the status code wasn't indicating success, or when the auth token has been invalid etc: Either String r .

Now, I don't want to care about it when I'm eval ing my components queries. I am only interested in happy path (expected errors like invalid logon attempt is considered happy path, just want to keep unexpected stuff out of it), and the errors should be handled uniformily and globally (sending some notification to the bus).

For this, I've created transformer stack:

type App = ReaderT Env (ExceptT String (Aff AppEffects))

Now, to use it with runUI , I needed to provide natural transformation to be used with hoist (unless I'm missing other possibilities):

runApp :: Env -> App ~> Aff AppEffects
runApp env app = do
  res <- runExceptT $ runReaderT app env
  case res of
    Right r -> pure unit
    Left err -> do Bus.write err env.bus
                   -- what to return here?

Because we're using ~> here, we're forced to retain the return type, but for the Left case I don't have it at hand!

How would one tackle such requirement? To reiterate - I only want to be able to 'cancel' the evaluation of my component query when the executed action encounters an error, but I want to do it silently and handle it from the top.

You have an exceptional case where the current thread can't continue, so the only thing to do would be to throw an exception in Aff using throwError :: forall eff a. Error -> Aff eff a throwError :: forall eff a. Error -> Aff eff a .

I came to conclusion that what I wanted to achieve was undesirable in fact. Making components query evaluation oblivious to the fact that an error have happened is nothing good (in the light that component might not be interested in handling the error fully, but at least doing something with its state to not end up broken).

Therefore, what I really need is some kind of helper that handle the error, and return a simple indication of the fact the it happened, so that component can proceed with that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM