I have the following method in one of my models to save a user record:
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
We can see that the rescue
block is heavy and such block is common in other actions as well. So I gave a thought to refactor this and move the rescue
block to a separate function. I want to implement something like this:
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
Any thoughts on an implementation as in above will be a great help.