Ruby rescue syntax error

9.4k views Asked by At

I have the following line of code that is giving me an error:

rescue Timeout::Error => e
    logs.puts("Rescued a timeout error...#{e}")
    email_ids_all.each do |email_delete|

      call= "/api/v2/emails/#{email_delete}/"
      uri= HTTParty.delete("https://www.surveys.com#{call}",
          :basic_auth => auth,
          :headers => { 'ContentType' => 'application/x-www-form-urlencoded', 'Content-Length' => "0" }
      )
      puts "Deleted email #{email_delete}".green
      log.puts("Deleted email #{email_delete}")
    end
    abort #abort entire script after deleting emails
  end

The error I am receiving is this:

syntax error, unexpected keyword_rescue, expecting $end
  rescue Timeout::Error => e
        ^

Essentially I am just trying to run an API delete call if the script times out. It doesn't seem to matter what I put in the block for rescue though, I receive the same error. What's wrong with my syntax on the rescue method?

1

There are 1 answers

5
AudioBubble On

The format for using rescue is as follows:

begin
  # Code you want to run that might raise exceptions
rescue YourExceptionClass => e
  # Code that runs in the case of YourExceptionClass
rescue ADifferentError => e
  # Code that runs in the case of ADifferentError
else
  # Code that runs if there was no error
ensure
  # Code that always runs at the end regardless of whether or not there was an error
end

Here is a question with lots more information: Begin, Rescue and Ensure in Ruby?.