I have tons of methods like this:
def lab1
setup
business_processing
rescue Exception => e
handle_error('custom error message for lab1', e)
end
def lab2
setup
business_processing
rescue Exception => e
handle_error('custom errror message for lab2', e)
end
I refactored the code above into something like this:
def lab1
with_setup_and_error_handling do
@error_message = 'error in lab1'
business_processing
end
end
def with_setup_and_error_handling(&block)
setup
block.call
rescue Exception => e
handle_error(@error_message, e)
end
The new code works but it causes error_message to be an instance variable. Is there a better way to refactor it?
When someone looks at the refactored code they see the instance variable, but it is not clear where it is being used so I am not happy with it.
Put the error message to be a argument of de setup method: