Search code examples
rubyexceptionreturnnameerrorrescue

Return a variable from a begin rescue error


The method

def exp_backoff up_to, url, header = {}                                                                                      
  tries = 0                                                                                                                  
  begin                                                                                                                      
    tries += 1                                                                                                               
    response = JSON.parse(open(url, header).read)                                                                            

    return response                                                                                                          
  rescue OpenURI::HTTPError => e                                                                                             
    if tries < up_to                                                                                                         
      sleep( 2 ** tries )                                                                                                    
      retry                                                                                                                  
    else                                                                                                                     
      return e                                                                                                               
    end                                                                                                                      
  end                                                                                                                        
end   

I'm calling exp_backoff expecting response to be returned, but it is not

  exp_backoff 2, status_url                                                                                                  
  session_token = response['session_token']

Getting this error

undefined local variable or method `response' for main:Object (NameError)

Solution

  • You didn't assign return value to anything. This will work:

    response = exp_backoff 2, status_url                                                                                                  
    session_token = response['session_token']
    

    Note however that your rescue part will return different object and response['session_token'] will raise an undefined method error. Maybe you should rethink your method? Most likely, instead of returning exception object, you should simply reraise it:

    rescue OpenURI::HTTPError                                                              
    if tries < up_to                                                                                                         
      sleep( 2 ** tries )                                                                                                    
      retry                                                                                                                  
    else                                                                                                                     
      raise                                                                                                       
    end