在模块内拯救特定类型的所有错误

编程入门 行业动态 更新时间:2024-10-23 01:30:05
本文介绍了在模块内拯救特定类型的所有错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我有一个模块,我正在执行一个项目的所有加密/解密任务。我想抓住这个模块中发生的任何 OpenSSL :: Cipher :: CipherError 异常,以便我可以处理它们。

是否可以执行类似

rescue_from OpenSSL :: Cipher :: CipherError,with with => :模块内部的密码错误

解决方案

我已经调查了一点,并提出了一个解决方案。你说你有一个模块,您可以在其中进行加密。我猜这个模块代表一个单身人士。但是,我的解决方案需要你有一个实例。

class Crypto def self.instance @__instance__ || = new end end

提取模块中的加密行为。

模块可加密 def加密#... 结束 解密# ... end end

创建一个处理异常的新模块。

模块ExceptionHandler 扩展ActiveSupport :: Concern 包含 include ActiveSupport :: Rescuable rescue_from StandardError,:with => :known_error end def handle_known_exceptions yield rescue => ex rescue_with_handler(ex)|| raise end def known_error(ex) Rails.logger.error[ExceptionHandler]异常#{ex.class}:#{ex.message} end end

所以现在你可以使用新定义的您的 Crypto 中的handle_known_exceptions 。这不是很方便,因为你没有多少收获。您仍然必须在每个方法中调用异常处理程序:

class Crypto include ExceptionHandler def print_bunnies handle_known_exceptions do File.open(bunnies) end end end

如果我们定义一个为我们这样做的委托者,则不需要这样做:

class CryptoDelegator include ExceptionHandler def initialize(target) @target = target end def method_missing (* args,& block) handle_known_exceptions do @ target.send(* args,& block) end end end

完全覆盖 Crypto 的初始化,以使用代理。

class Crypto include Encryptable def self.new(* args,& block) CryptoDelegator.new(super) end def self.inst ance @__instance__ || = new end end

就是这样!

I have a module in which I am performing all of my encryption/decryption tasks for a project. I would like to catch any OpenSSL::Cipher::CipherError exceptions that occur in this module so that I can handle them.

Is it possible to do something like

rescue_from OpenSSL::Cipher::CipherError, :with => :cipher_error

inside of a module?

解决方案

I've investigated a little and came with a solution. You said you have a module in which you do your encryption. I'm guessing that module represents a singleton. My solution, however, requires you have an instance instead.

class Crypto def self.instance @__instance__ ||= new end end

Extract encryption behavior in a module.

module Encryptable def encrypt # ... end def decrypt # ... end end

Create a new module that handles exceptions.

module ExceptionHandler extend ActiveSupport::Concern included do include ActiveSupport::Rescuable rescue_from StandardError, :with => :known_error end def handle_known_exceptions yield rescue => ex rescue_with_handler(ex) || raise end def known_error(ex) Rails.logger.error "[ExceptionHandler] Exception #{ex.class}: #{ex.message}" end end

So now you can use the newly defined handle_known_exceptions inside your Crypto. This is not very convenient because you haven't gained much. You still have to call the exception handler inside every method:

class Crypto include ExceptionHandler def print_bunnies handle_known_exceptions do File.open("bunnies") end end end

No need to do this if we define a delegator that does that for us:

class CryptoDelegator include ExceptionHandler def initialize(target) @target = target end def method_missing(*args, &block) handle_known_exceptions do @target.send(*args, &block) end end end

Completely override the initialization of Crypto, to use the delegator instead.

class Crypto include Encryptable def self.new(*args, &block) CryptoDelegator.new(super) end def self.instance @__instance__ ||= new end end

And that's it!

更多推荐

在模块内拯救特定类型的所有错误

本文发布于:2023-11-15 04:03:42,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1590976.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:模块   错误   类型

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!