# ApiException not getting triggered

**URL:** <https://community.hubspot.com/t/apiexception-not-getting-triggered/99765>\
**Category:** APIs & Integrations\
**Tags:** python, object\
**Created:** [February 8, 2024, 2:06pm UTC](https://community.hubspot.com/t/apiexception-not-getting-triggered/99765 "2024-02-08T14:06:21Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![andriushivehr](https://avatars.discourse-cdn.com/v4/letter/a/dfb087/32.png) [@andriushivehr](https://community.hubspot.com/u/andriushivehr)\
**Post date:** [February 8, 2024, 2:06pm UTC](https://community.hubspot.com/t/apiexception-not-getting-triggered/99765/1 "2024-02-08T14:06:21Z")

</div>

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?

```lia-code-sample
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?

---

<div class="post-metadata">

**Author:** ![dsmarion](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/dsmarion/32/7509_2.png) [@dsmarion](https://community.hubspot.com/u/dsmarion)\
**Post date:** [February 8, 2024, 10:10pm UTC](https://community.hubspot.com/t/apiexception-not-getting-triggered/99765/2 "2024-02-08T22:10:28Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![andriushivehr](https://avatars.discourse-cdn.com/v4/letter/a/dfb087/32.png) [@andriushivehr](https://community.hubspot.com/u/andriushivehr)\
**Post date:** [February 12, 2024, 2:36pm UTC](https://community.hubspot.com/t/apiexception-not-getting-triggered/99765/3 "2024-02-12T14:36:22Z")

</div>

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?)

```lia-code-sample
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!

```lia-code-sample
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

```

---

<div class="post-metadata">

**Author:** ![dsmarion](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/dsmarion/32/7509_2.png) [@dsmarion](https://community.hubspot.com/u/dsmarion)\
**Post date:** [February 12, 2024, 2:47pm UTC](https://community.hubspot.com/t/apiexception-not-getting-triggered/99765/4 "2024-02-12T14:47:29Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![andriushivehr](https://avatars.discourse-cdn.com/v4/letter/a/dfb087/32.png) [@andriushivehr](https://community.hubspot.com/u/andriushivehr)\
**Post date:** [February 13, 2024, 10:03am UTC](https://community.hubspot.com/t/apiexception-not-getting-triggered/99765/5 "2024-02-13T10:03:54Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![dsmarion](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/dsmarion/32/7509_2.png) [@dsmarion](https://community.hubspot.com/u/dsmarion)\
**Post date:** [February 13, 2024, 1:41pm UTC](https://community.hubspot.com/t/apiexception-not-getting-triggered/99765/6 "2024-02-13T13:41:52Z")

</div>

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?
