How to handle exceptions when creating contacts with HubSpot Ruby gem

I’m working with a Ruby on Rails application and I’m using the HubSpot Ruby gem to create new contacts. The contact creation works fine when everything goes smoothly, but I’m struggling to figure out how to properly catch and handle errors when something goes wrong.

Here’s my current implementation:

def add_contact_to_crm(user_data)
  puts "Adding contact to CRM..."
  @user_info = user_data

  @given_name = @user_info.full_name.split(" ").first || "Unknown"
  @family_name = @user_info.full_name.split(" ").last || "Unknown"
  @contact_number = @user_info.phone_number || "Not provided"
  @email_address = @user_info.email_address || "Not provided"
  @source = @user_info.lead_source || "website"
  @location = Region.find(@user_info.region_id).title || "Unknown"
  @notes = @user_info.comments || "No message"

  new_contact = Hubspot::Contact.create!(@email_address, {
    firstname: @given_name,
    lastname: @family_name,
    phone: @contact_number,
    email: @email_address,
    source: @source,
    location: @location,
    notes: @notes
  })

  # How do I catch errors at this point?
end

What’s the best way to implement error handling in this scenario? I want to catch any API failures or validation errors that might occur during the contact creation process.

Also handle network timeouts! HubSpot’s API gets slow sometimes and your app will hang without a timeout. I wrap the whole thing with a timeout or set it during client init. Saved my butt when their servers went down in production.

Also consider using Hubspot::Contact.create instead of create! for better error handling. The bang method throws exceptions right away, while the regular version returns nil when it fails - gives you more control.

new_contact = Hubspot::Contact.create(@email_address, {
  firstname: @given_name,
  lastname: @family_name,
  phone: @contact_number,
  email: @email_address,
  source: @source,
  location: @location,
  notes: @notes
})

if new_contact.nil?
  Rails.logger.warn "Failed to create HubSpot contact for #{@email_address}"
  return { success: false, error: "Contact creation failed" }
end

This works great when you want to keep processing even if the CRM integration breaks. Really useful in signup flows where you don’t want third-party API problems blocking user registration.

Wrap your HubSpot API call in a begin-rescue block to catch exceptions. I always handle both general API errors and specific validation issues when working with the HubSpot gem.

Replace your Hubspot::Contact.create! call with this:

begin
  new_contact = Hubspot::Contact.create!(@email_address, {
    firstname: @given_name,
    lastname: @family_name,
    phone: @contact_number,
    email: @email_address,
    source: @source,
    location: @location,
    notes: @notes
  })
rescue Hubspot::RequestError => e
  Rails.logger.error "HubSpot API error: #{e.message}"
  return false
rescue StandardError => e
  Rails.logger.error "Unexpected error creating contact: #{e.message}"
  return false
end

Hubspot::RequestError catches most API issues - auth failures, rate limiting, invalid data. The StandardError is your safety net for everything else.