Yesod的例外 [英] Exceptions in Yesod

查看:165
本文介绍了Yesod的例外的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用了一个非常原始的形式的 ipc (telnet并以特定的顺序发送一些字符串)。我已经剔除了它,现在使用 JSON 将消息传递给$ code> Yesod 服务器。但是,有一些我非常喜欢我的设计,我不知道现在的选择。

I had made a daemon that used a very primitive form of ipc (telnet and send a String that had certain words in a certain order). I snapped out of it and am now using JSON to pass messages to a Yesod server. However, there were some things I really liked about my design, and I'm not sure what my choices are now.

这是我在做什么:

buildManager :: Phase -> IO ()
buildManager phase = do
  let buildSeq = findSeq phase
      jid = JobID $ pack "8"
      config = MkConfig $ Just jid
  flip C.catch exceptionHandler $ 
  runReaderT (sequence_ $ buildSeq <*> stages) config
  -- ^^ I would really like to keep the above line of code, or something like it.
  return ()

buildSeq中的每个函数看起来像这样

each function in buildSeq looked like this

foo :: Stage -> ReaderT Config IO ()

data Config = MkConfig (Either JobID Product) BaseDir JobMap

JobMap 是一个 TMVar Map ,用于跟踪有关当前作业的信息。

JobMap is a TMVar Map that tracks information about current jobs.

所以现在,我有一个处理程序,这些都像这样

so now, what I have are Handlers, that all look like this

foo :: Handler RepJson

foo 表示我的守护进程的命令,处理程序可能需要处理不同的JSON对象。

foo represents a command for my daemon, each handler may have to process a different JSON object.

我想做的是发送一个 JSON 对象代表成功,另一个JSON对象可以提供有关某些异常的信息。

What I would like to do is send one JSON object that represents success, and another JSON object that espresses information about some exception.

我希望 foo 的帮助函数可以返回一个两个,但是我不知道我是如何得到的,加上终止对我的操作列表的评估的能力, buildSeq

I would like foos helper function to be able to return an Either, but I'm not sure how I get that, plus the ability to terminate evaluation of my list of actions, buildSeq.

这是我看到的唯一选择

1)确保 exceptionHandle r 在处理程序中。在 App 记录中放入 JobMap 。使用 getYesod 更改 JobMap 中的适当值,指示有关异常的详细信息,
,然后可以通过 foo

1) make sure exceptionHandler is in Handler. Put JobMap in the App record. Using getYesod alter the appropriate value in JobMap indicating details about the exception, which can then be accessed by foo

有更好的方法吗?

我的其他选择是什么?

编辑:为了清楚起见,我将解释 Handler RepJson 的作用。服务器需要一些方法来接受诸如 build stop / / code> 报告。客户端需要了解这些命令的结果。我选择了JSON作为服务器和客户端相互通信的媒介。我正在使用Handler类型来管理JSON输入/输出,而没有更多。

For clarity, I will explain the role ofHandler RepJson. The server needs some way to accept commands such as build stop report. The client needs some way of knowing the results of these commands. I have chosen JSON as the medium with which the server and client communicate with each other. I'm using the Handler type just to manage the JSON in/out and nothing more.

推荐答案

哲学上说,在Haskell / Yesod世界你想传递价值观,而不是向后退。所以不用让处理程序返回一个值,让它们向前转到进程的下一步,这可能是生成一个异常。

Philosophically speaking, in the Haskell/Yesod world you want to pass the values forward, rather than return them backwards. So instead of having the handlers return a value, have them call forwards to the next step in the process, which may be to generate an exception.

记住你可以捆绑任何数量的未来操作都放在一个对象中,所以你可以将一个继承对象传递给你的处理程序和foos,这些对象基本上告诉他们:完成后,运行这个代码块。这样他们就可以无效,没有任何回报。

Remember that you can bundle any amount of future actions into a single object, so you can pass a continuation object to your handlers and foos that basically tells them, "After you are done, run this blob of code." That way they can be void and return nothing.

这篇关于Yesod的例外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆