Error handling Clojure-引用defrecord函数
如何引用记录的函数 在上下文中,我使用Stuart Sierra的组件。所以我有这样的记录:Error handling Clojure-引用defrecord函数,error-handling,clojure,architecture,Error Handling,Clojure,Architecture,如何引用记录的函数 在上下文中,我使用Stuart Sierra的组件。所以我有这样的记录: (defrecord MyComponent [] component/Lifecycle (start [component] ...) (stop [component] ...) 然而,自述文件中指出: …您可以将stop的主体包装为一个try/catch,忽略所有内容 例外情况。这样,停止一个组件的错误将不会阻止 防止其他部件干净地关闭 然而,我想用这个。现在我如
(defrecord MyComponent []
component/Lifecycle
(start [component]
...)
(stop [component]
...)
然而,自述文件中指出:
…您可以将stop的主体包装为一个try/catch,忽略所有内容
例外情况。这样,停止一个组件的错误将不会阻止
防止其他部件干净地关闭
然而,我想用这个。现在我如何引用stop函数来与Dire一起使用?您不包装stop,而是包装stop的主体-也就是说,除了参数声明之外的所有内容都包装在您的Dire/with处理程序中!块或任何其他您喜欢的错误捕获方法
(defstruct MyComponent []
component/Lifecycle
(start [component]
(try (/ 1 0)
(catch Exception e)
(finally component))))
请注意,无论如何处理错误,如果不从start方法返回组件,就会破坏系统。如果不包装stop,则包装stop的主体-也就是说,除了参数声明之外的所有内容都包装在您的dire/with处理程序中!块或任何其他您喜欢的错误捕获方法
(defstruct MyComponent []
component/Lifecycle
(start [component]
(try (/ 1 0)
(catch Exception e)
(finally component))))
请注意,无论您如何处理错误,如果您不从启动方法返回组件,您都将破坏系统。有两个自然选项: 您可以使用Dire来处理组件/停止和可能启动的错误: 这样,您将致力于处理系统中可能使用的所有组件的错误,以及应用程序中任何地方对组件/停止的任何调用 您可以引入一个顶级函数来处理组件的停止逻辑,将其注册到Dire中,让组件/停止实现仅委托给它,或者以类似方式处理启动:
(defn start-my-component [component]
…)
(defn stop-my-component [component]
…)
(dire.core/with-handler! #'start-my-component
…)
(dire.core/with-handler! #'stop-my-component
…)
(defrecord MyComponent […]
component/Lifecycle
(start [component]
(start-my-component component))
(stop [component]
(stop-my-component component)))
有两种自然选择: 您可以使用Dire来处理组件/停止和可能启动的错误: 这样,您将致力于处理系统中可能使用的所有组件的错误,以及应用程序中任何地方对组件/停止的任何调用 您可以引入一个顶级函数来处理组件的停止逻辑,将其注册到Dire中,让组件/停止实现仅委托给它,或者以类似方式处理启动:
(defn start-my-component [component]
…)
(defn stop-my-component [component]
…)
(dire.core/with-handler! #'start-my-component
…)
(dire.core/with-handler! #'stop-my-component
…)
(defrecord MyComponent […]
component/Lifecycle
(start [component]
(start-my-component component))
(stop [component]
(stop-my-component component)))
这些选项听起来不错,但是这是否意味着如果不包装单独的方法,就无法引用特定记录中的所有停止方法,对吗?这些选项听起来不错,但是这是否意味着如果不包装单独的方法,就无法引用特定记录中的所有停止方法,对吗?