I have a rails code snippet that does a rake file from a model.
Rake.application.rake_require '../../lib/tasks/master_load'
fork do
results = capture_stdout {Rake.application['db:seed:excel:to_yaml'].invoke}
end
capture_stdout is for printing out the logs
def self.capture_stdout
s = StringIO.new
oldstdout = $stdout
$stdout = s
yield
s.string
Rails.logger.info "#{s.string}"
ensure
$stdout = oldstdout
end
it works, but when the rake encounters error and fails. it fails silently. is there a way to know if error has occured and perhaps get the error log?
Use rescue to catch and handle error
def self.capture_stdout
s = StringIO.new
oldstdout = $stdout
$stdout = s
yield
s.string
Rails.logger.info "#{s.string}"
rescue Exception => e
# handle e - exception object
ensure
$stdout = oldstdout
end
Ensure in this case silently makes action after exception handling