How can I manage errors when using the HubSpot Ruby gem to create contacts?

I’m using a Ruby wrapper for the HubSpot API in my Rails app, and while I’m able to create contacts successfully, I’m uncertain about how to handle errors effectively when they occur.

Here’s the code I’m currently working with:

def add_hubspot_lead(client_data)
  puts "Adding lead to HubSpot..."
  @client_info = client_data

  @given_name = @client_info.full_name.split(" ").first || "Unknown"
  @family_name = @client_info.full_name.split(" ").last || "Unknown"
  @contact_phone = @client_info.phone_number || "N/A"
  @contact_email = @client_info.email_address || "N/A"
  @lead_source = @client_info.source || "website"
  @location = Area.find(@client_info.area_id).title || "N/A"
  @inquiry = @client_info.notes || "N/A"

  new_contact = Hubspot::Contact.create!(@contact_email, {
    firstname: @given_name,
    lastname: @family_name,
    phone: @contact_phone,
    email: @contact_email,
    source: @lead_source,
    location: @location,
    notes: @inquiry
  })

  # How can I handle errors here?
end

What’s the best way to manage API errors or validation problems in this situation?

Had the same problem integrating HubSpot. Don’t just catch exceptions - add retry logic because their API gets flaky during peak hours. I use exponential backoff: retry 3 times with longer delays each attempt. Rate limiting errors return different status codes, so handle those separately. Log HubSpot’s full error response - their messages usually tell you exactly which field broke validation. Add a sync status flag to your database so you can spot failed records and retry them with a background job later.

The create! method throws exceptions when it fails, so you need error handling. I’ve hit this before - rescuing Hubspot::RequestError catches API issues, but also grab StandardError as backup since network timeouts happen. Also validate your data first before sending to HubSpot. I see you’re using || "N/A" for fallbacks, but HubSpot might reject those values. Consider switching to the regular create method instead - it just returns false on failure instead of throwing exceptions, so you get better control.

try wrapping the HubSpot call in a begin/rescue block. if the API fails, catch a Hubspot::RequestError to log it or inform the user about the error.

Had the same headaches building our CRM integration. Validate emails before sending them to HubSpot, as their API provides poor error messages for bad formats. Duplicates also pose a challenge; HubSpot returns a specific error code when an email already exists, so catch that and update the existing contact instead of allowing it to crash. Your instance variable setup seems overly complex; instead, pass the clean data directly to the API and handle errors right there. Additionally, be aware that HubSpot’s validation may vary based on your account type, so it’s crucial to test everything in production first.

double-check your data before calling the HubSpot API - empty emails will crash it. and ditch that create! method, it’s risky. use regular create and check the return value instead of constantly catching exceptions.

This topic was automatically closed 4 days after the last reply. New replies are no longer allowed.