是否可以执行类似
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_errorinside 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 endExtract encryption behavior in a module.
module Encryptable def encrypt # ... end def decrypt # ... end endCreate 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 endSo 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 endNo 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 endCompletely 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 endAnd that's it!
更多推荐
在模块内拯救特定类型的所有错误
发布评论