Rails 4: HubSpot API 错误检查
Rails 4: HubSpot API error checking
我正在使用 Rails wrapper for the HubSpot API,虽然我能够成功创建联系人,但我似乎无法处理错误。
def createHubSpotContact(potential_client)
puts "creating hubspot contact..."
@potential_client = potential_client
@first_name = @potential_client.name.split(" ").first || "N/A"
@last_name = @potential_client.name.split(" ").last || "N/A"
@phone = @potential_client.phone || "N/A"
@email = @potential_client.email || "N/A"
@referrer = @potential_client.referrer || "other"
@city = Location.find(@potential_client.location_id).name || "N/A"
@message = @potential_client.message || "N/A"
contact = Hubspot::Contact.create!(@email, {
firstname: @first_name,
lastname: @last_name,
phone: @phone,
email: @email,
referrer: @referrer,
city: @city,
message: @message
})
# What can I do to handle an error here?
end
如果联系无效,bang 方法 create!
应该会引发错误。因此,您永远不会通过创建来处理错误。
查看 gem source,create!
方法调用 HubSpot API:
response = Hubspot::Connection.post_json(CREATE_CONTACT_PATH, params: {}, body: post_data )
在Connection#post_json
,
raise(Hubspot::RequestError.new(response)) unless response.success?
如果创建联系人时出现问题,则会引发 RequestError
。所以,你可以捕捉到:
begin
contact = Hubspot::Contact.create!(@email, {
...
rescue Hubspot::RequestError
# Handle an error here.
end
处理此问题的一种方法是 return false from createHubSpotContact
:
rescue Hubspot::RequestError
return false
end
然后,您可以将它与
一起使用
if contact = createHubSpotContact(potential_client)
...
else
#failure
end
我正在使用 Rails wrapper for the HubSpot API,虽然我能够成功创建联系人,但我似乎无法处理错误。
def createHubSpotContact(potential_client)
puts "creating hubspot contact..."
@potential_client = potential_client
@first_name = @potential_client.name.split(" ").first || "N/A"
@last_name = @potential_client.name.split(" ").last || "N/A"
@phone = @potential_client.phone || "N/A"
@email = @potential_client.email || "N/A"
@referrer = @potential_client.referrer || "other"
@city = Location.find(@potential_client.location_id).name || "N/A"
@message = @potential_client.message || "N/A"
contact = Hubspot::Contact.create!(@email, {
firstname: @first_name,
lastname: @last_name,
phone: @phone,
email: @email,
referrer: @referrer,
city: @city,
message: @message
})
# What can I do to handle an error here?
end
如果联系无效,bang 方法 create!
应该会引发错误。因此,您永远不会通过创建来处理错误。
查看 gem source,create!
方法调用 HubSpot API:
response = Hubspot::Connection.post_json(CREATE_CONTACT_PATH, params: {}, body: post_data )
在Connection#post_json
,
raise(Hubspot::RequestError.new(response)) unless response.success?
如果创建联系人时出现问题,则会引发 RequestError
。所以,你可以捕捉到:
begin
contact = Hubspot::Contact.create!(@email, {
...
rescue Hubspot::RequestError
# Handle an error here.
end
处理此问题的一种方法是 return false from createHubSpotContact
:
rescue Hubspot::RequestError
return false
end
然后,您可以将它与
一起使用if contact = createHubSpotContact(potential_client)
...
else
#failure
end