0

I am running a http request by using the open-uri method. The following error message is intermittently getting rather than the expected output.

An error of type NoMethodError happened, message is undefined method `[]' for nil:NilClass

er ?

And my code is given below,

request_uri = "my url here"
request_query = ''
url = "#{request_uri}#{request_query}"

begin
  buffer = open(request_uri,
             'app-key' => ENV['API_KEY'],
             'app-token' => ENV['API_TOKEN'],
             'trail-Token' => ENV['TRAIL_TOKEN']).read
  parsed = JSON.parse(buffer)
  events = parsed['events']
  event = Array(events).last
  message = event['message']
  otp = message[-5,4]
  print "otp", otp
rescue Exception => ex
  puts "An error of type #{ex.class} happened, message is #{ex.message}"
  retry
end

puts "otp returned", otp

Since I am new to ruby don't know why the response is getting like this. Once I get the response properly the next time when I run the script er ? is the response. Someone please give a helping hand to fix the issue.

Community
  • 1
  • 1
Muneer Muhammed
  • 883
  • 3
  • 10
  • 29
  • Remove the `rescue` block to see the whole error message (including line number and backtrace). See [Rescue StandardError, Not Exception](https://robots.thoughtbot.com/rescue-standarderror-not-exception) – Stefan May 26 '16 at 06:08
  • Rescuing `Exception` is usually an anti-pattern in Ruby. Please see: http://stackoverflow.com/questions/10048173/why-is-it-bad-style-to-rescue-exception-e-in-ruby – Wayne Conrad May 26 '16 at 15:04

1 Answers1

1

The begin rescue block is pretty big here, you are losing your exception details, including linenumbers.

Effectively, at some point what is happening is you are calling [] on an object that is nil, and it fails. Which point? You wont be able to see without the stack trace or a smaller block.

As @Stefan mentioned, please dont catch exception, if you are not sure what you will get, leave it blank and ruby will do the best thing for you and grab the right part of the exception traces, it just makes life hard, see: Why is it a bad style to `rescue Exception => e` in Ruby?

If you wanna see the exact line that the exception is caused on, and I would also definitely limit retries.

begin
  retries ||= 0
  ...

rescue => ex
  puts "An error of type #{ex.class} happened, message is #{ex.message}"
  puts ex.backtrace
  retry if (retries += 1) < 3
end
Community
  • 1
  • 1
Luke Exton
  • 3,506
  • 2
  • 19
  • 33
  • Thanks for your helping hand @Luke. But even after that, I am getting the **er ?** response intermittently. Why is it so ? – Muneer Muhammed May 26 '16 at 09:26
  • For that, you would need to dive into the particular error. The likely cause of the error is that there are network issues or your API is not responding with json of the expected format 100% of the time. Which is causing errors to bubble up in your downstream code. To get more info, you would need to validate the input you are receiving from your api to make sure it is expected first. – Luke Exton May 26 '16 at 10:43
  • OK. Thank you very much for the instructions. – Muneer Muhammed May 26 '16 at 11:49