Hi,
I’m trying to understand how the ApiException works in the Python API framework. I’ve tried fetching a property that doesn’t exist - but the ApiException isn’t getting triggered. Any ideas?
from hubspot.crm.deals import ApiException as ApiException_deals
<..> rest of the code <..>
try:
api_response = client.crm.deals.basic_api.get_page(
archived=False,
limit=page_limit,
after=next_page,
properties=["doesntexist_1", "doesntexist2", "dealname"],
associations=(
["companies"] if DEALS_INCLUDE_ASSOCIATED_COMPANY_ID else []
),
)
except ApiException_deals as error:
print(
f"{error}\n"
+ "API Exception when fetching the data (see API error message above)\n"
+ f"Fetching Stage: FETCHING_DEALS | Limit = {page_limit} | After = {next_page}\n"
+ "Check the API call and run the code again\n"
)
In the above example, doesntexist_1 and doesntexist_2 should trigger an error - that is unless non-existent properties are ignored? In that case, when is the ApiException triggered?
The properties that don’t exist are ignored, you’re absolutely correct. Your exception would trigger if the call fails or otherwise could not be completed.
Hey! Thanks for confirming the above. For some reason I couldn’t reply before - I was getting an error saying that my message couldn’t be posted and that I should try later.
I have one more question if you don’t mind -
Is there any difference between the below versions of the code (as in, will they work the same?)
from hubspot.crm.deals import ApiException
while True:
try:
if fetching_stage == FetchingStage.FETCHING_DEALS:
api_response = client.crm.deals.basic_api.get_page(....)
elif fetching_stage == FetchingStage.FETCHING_COMPANIES:
api_response = client.crm.companies.basic_api.get_page(.....)
<...> the rest of the code
except ApiException as error:
print("error - ", error)
Or do I have to do this in this specific case? Given that non-existent properties are ignored, I have no idea how to trigger the ApiException hence why I didn’t test it myself - apologies!
from hubspot.crm.deals import ApiException as ApiException_deals
from hubspot.crm.companies import ApiException as ApiException_companies
while True:
if fetching_stage == FetchingStage.FETCHING_DEALS:
try:
api_response = client.crm.deals.basic_api.get_page(....)
except ApiException_deals as error:
print("error - deals", error)
elif fetching_stage == FetchingStage.FETCHING_COMPANIES:
try:
api_response = client.crm.companies.basic_api.get_page(.....)\
except ApiException_companies as error:
print("error - companies", error)
<...> the rest of the code
Well, first I don’t program in that language so I cannot comment on whether the syntax is correct (I use node.js) but both versions look like they would work fine to me. The only difference is the first one has a general try/catch structure around the whole code while the second has a separate try/catch for each API call which allows you to customize each error message separately. Personal preference I would say.
Thanks for the quick reply! Structure-wise the code is fine both ways, but I’m just wondering if importing ApiExceptiom from, say, the companies part of the library will also work when we’re trying to catch the ApiException when the API errors out fetching, say, deals data
Again I don’t know those libraries at all so I can’t say for certain. But I would think that the ApiException object is named that because an exception is an exception no matter what generated it. I would think your library uses the same object no matter where in the library its used. You would need to dig into that code and look to be sure. What code library is that just for FYI?