0

Currently for a project I am using api_auth gem. I am hitting on an external api in a function by making a signed request. Currently my code looks like this.

access_id = "someKey"
secret_key = "someRandomGeneratedKey"
request = RestClient::Request.new(url: 'serverUrl', method: get, headers:'headers')

@signed_request = ApiAuth.sign!(access_id, secret_key, request)
@signed_request.execute

I am unable to apply any error handling code to that last statement where I execute the request. Any suggestions?

RockStar
  • 1,304
  • 2
  • 13
  • 35
wallydrag
  • 1,924
  • 1
  • 11
  • 9

1 Answers1

0

You have to use begin rescue block to exception handling.

begin
    #TODO Exception occurring statement
rescue 
    #Exception handling code
end

As you should only rescue specific exception, rather than all. So as your code suggest that you are using rest_client & api-auth gem so from documentation you can get list of exception that this gem raise.

Example - For rest_client below exceptions need to be handled. (probably this is the solution of your problem, just replace last line as given below)

begin 
    @signed_request.execute
rescue RestClient::ExceptionWithResponse, URI::InvalidURIError, RestClient::InternalServerError => err
    p err
end

There are few Exceptions also generated by api-auth gem also like ApiAuth::ApiAuthError, ApiAuth::UnknownHTTPRequest so you need to rescue all this exceptions.

Reference link -

http://www.rubydoc.info/gems/api-auth/1.4.0/

Thanks!

RockStar
  • 1,304
  • 2
  • 13
  • 35