cancel
Showing results forย 
Search instead forย 
Did you mean:ย 

Do not break foreach loop when API fails

bhavesh-patel
Deputy Chef III
Deputy Chef III

Issue is inside custom connector ruby code.

Context: I have a list of companies in an array then I am using .each method to loop every company and then hitting an api call on every company.

Problem: Now one api call is failed for a company and code breaks. I don't want code to break, instead I want loop to continue as we use try catch in many languages to prevent breaking of loop. How to acheive the same in Custom Connector SDK using ruby.

2 ACCEPTED SOLUTIONS

Ruby exception handling is unfortunately also not supported in the SDK-Code.

View solution in original post

There is no support for exception handling so I have move this code on the recipe level and then I used repeat action in a parent recipe on my company array and then every action goes to a child recipe where I use monitor block of recipe to handle exception from API. Then I move every error into lookup table.

View solution in original post

5 REPLIES 5

bhavesh-patel
Deputy Chef III
Deputy Chef III

My API call is as below:

company_ids.each {
                    |company_id|
                    puts("company_id: #{company_id}")
                    companyobject = post("#{base_url}/api/v2/labor-model/location/#{company_id}/run").payload(start:start_unix, end:end_unix).headers("Authorization": auth_header).
                    after_error_response(400, 401, 404, 417, 500) do |code, body, header, message|
                        error("#{message}: #{body}")
                    end
                    puts("companyobject: #{companyobject}")
                }

Hey @bhavesh-patel,

I think this page on ruby loops could be helpful for you: https://www.tutorialspoint.com/ruby/ruby_loops.htm

You can also find more variations of 'each' here: https://docs.workato.com/developing-connectors/sdk/sdk-reference/ruby_methods.html#each

Please let me know if you find your solution within those pages!

Cheers,
Meghan

gary1
Executive Chef III
Executive Chef III

I'm not sure if begin/rescue or catch/throw are supported in a custom connector, but you should give them a try. They aren't supported in the Ruby recipe connector (I tried and confirmed with Workato support) but the connector SDK tends to have broader Ruby support.

https://www.geeksforgeeks.org/ruby-exception-handling/

Here's an example:

companies.each { | company | 
   x = {}

   begin
      x.split(';') # example that will throw error
      # put your API call here
   rescue
      puts "error is skipped"
   end
}

Ruby exception handling is unfortunately also not supported in the SDK-Code.