如何将救援块移动到方法
How to move a rescue block to a method
我的一个模型中有以下方法来保存用户记录:
def save_user(params)
begin
save_user_details(params)
rescue ActiveRecord::RecordInvalid => ex
{ success: false, errors: ex.messages }
rescue Exception => ex
Rails.logger.info(ex.message)
Rails.logger.info(ex.backtrace.join(‘\n’)
{ success: false, errors: ’Some error occurred.}
end
end
我们可以看到rescue
块很重,这种块在其他动作中也很常见。所以我想重构这个并将 rescue
块移动到一个单独的函数。我想实现这样的东西:
def save_user(params)
begin
save_user_details(params) # my method to save the details
handle_the_exception # not sure how to implement this
end
def handle_the_exception
# How to handle here ?
end
任何关于上述实现的想法都会有很大帮助。
像这样:
def save_user(params)
handle_exception do
save_user_details(params) # my method to save the details
end
end
def handle_exception(&block)
begin
block.call
rescue => ex
Rails.logger.info(ex.message)
Rails.logger.info(ex.backtrace.join("\n")
{ success: false, errors: "Some error occurred."}
end
end
在 IRB 中:
2.2.0 :021 > def handle_exception(&block)
2.2.0 :022?> begin
2.2.0 :023 > block.call
2.2.0 :024?> rescue => ex
2.2.0 :025?> puts ex.inspect
2.2.0 :026?> puts "handled"
2.2.0 :027?> end
2.2.0 :028?> end
=> :handle_exception
2.2.0 :029 > handle_exception do
2.2.0 :030 > raise "hell"
2.2.0 :031?> puts "Don't reach me"
2.2.0 :032?> end
#<RuntimeError: hell>
handled
=> nil
你可以试试
def save_user(params)
save_user_details(params)
rescue => e
handle_exception(e)
end
def handle_exception(error)
Rails.logger.info(error.message)
Rails.logger.info(error.backtrace.join(‘\n’))
{ success: false, errors: error.messages }
end
还有 here 为什么你不应该拯救 Exception
我的一个模型中有以下方法来保存用户记录:
def save_user(params)
begin
save_user_details(params)
rescue ActiveRecord::RecordInvalid => ex
{ success: false, errors: ex.messages }
rescue Exception => ex
Rails.logger.info(ex.message)
Rails.logger.info(ex.backtrace.join(‘\n’)
{ success: false, errors: ’Some error occurred.}
end
end
我们可以看到rescue
块很重,这种块在其他动作中也很常见。所以我想重构这个并将 rescue
块移动到一个单独的函数。我想实现这样的东西:
def save_user(params)
begin
save_user_details(params) # my method to save the details
handle_the_exception # not sure how to implement this
end
def handle_the_exception
# How to handle here ?
end
任何关于上述实现的想法都会有很大帮助。
像这样:
def save_user(params)
handle_exception do
save_user_details(params) # my method to save the details
end
end
def handle_exception(&block)
begin
block.call
rescue => ex
Rails.logger.info(ex.message)
Rails.logger.info(ex.backtrace.join("\n")
{ success: false, errors: "Some error occurred."}
end
end
在 IRB 中:
2.2.0 :021 > def handle_exception(&block)
2.2.0 :022?> begin
2.2.0 :023 > block.call
2.2.0 :024?> rescue => ex
2.2.0 :025?> puts ex.inspect
2.2.0 :026?> puts "handled"
2.2.0 :027?> end
2.2.0 :028?> end
=> :handle_exception
2.2.0 :029 > handle_exception do
2.2.0 :030 > raise "hell"
2.2.0 :031?> puts "Don't reach me"
2.2.0 :032?> end
#<RuntimeError: hell>
handled
=> nil
你可以试试
def save_user(params)
save_user_details(params)
rescue => e
handle_exception(e)
end
def handle_exception(error)
Rails.logger.info(error.message)
Rails.logger.info(error.backtrace.join(‘\n’))
{ success: false, errors: error.messages }
end
还有 here 为什么你不应该拯救 Exception