问题描述
我有一个模块,我正在执行一个项目的所有加密/解密任务。我想抓住这个模块中发生的任何 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!
这篇关于在模块内拯救特定类型的所有错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!