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.
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.
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.