I’m working with a HubSpot Ruby wrapper in my Rails 4 application. The contact creation works fine, but I’m struggling with proper error handling. When something goes wrong during the API call, my application doesn’t handle it gracefully.
def build_hubspot_user(client_data)
puts "Building new hubspot user..."
@client_data = client_data
@given_name = @client_data.full_name.split(" ").first || "Unknown"
@family_name = @client_data.full_name.split(" ").last || "Unknown"
@contact_number = @client_data.phone_number || "N/A"
@email_address = @client_data.email_address || "N/A"
@source = @client_data.traffic_source || "direct"
@location = Region.find(@client_data.region_id).title || "Unknown"
@notes = @client_data.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 and manage errors at this point?
end
What’s the best way to handle API failures and exceptions in this scenario?