Skip to content

Instantly share code, notes, and snippets.

@stmcallister
Last active October 28, 2022 18:56
Show Gist options
  • Save stmcallister/59560902ddd9b1abeb5056dee6a0f8f9 to your computer and use it in GitHub Desktop.
Save stmcallister/59560902ddd9b1abeb5056dee6a0f8f9 to your computer and use it in GitHub Desktop.
Accessing Smartsheet ApiError object in Python
#!/usr/bin/env python
import smartsheet
import os
import json
ss_client = smartsheet.Smartsheet(os.environ['SMARTSHEET_ACCESS_TOKEN'])
ss_client.errors_as_exceptions()
sheet_ID = xxxxxxxxxxxxxxxx
try:
my_sheet = ss_client.Sheets.get_sheet(sheet_ID)
print(my_sheet)
except smartsheet.exceptions.SmartsheetException as e:
# SmartsheetException is the base class for all of the exceptions raised by the SDK.
# The two most common types of SmartsheetException are ApiError and HttpError. After trapping the exception, first
# determine the exception type using an ‘isinstance’ test.
#
# In this example, the ApiError exception has an Error class object accessible through the ‘error’ property, and
# then that in turn points to an ErrorResult class accessible through the result property.
#
# The details of the API error are stored in that ErrorResult.
if isinstance(e, smartsheet.exceptions.ApiError):
print(e.error.result.error_code)
print(e.error.result.message)
print(e.error.result.name)
print(e.error.result.recommendation)
@poxford3
Copy link

Trying this at the moment and not quite sure that the exception is being raised correctly. Whenever I run the code and I get an error for too many requests, it continues displaying this instead of stopping and following the "except" block. Has this changed since you've posted this?

Please let me know if you have any information on this.

@Bmcalpine
Copy link

Trying this at the moment and not quite sure that the exception is being raised correctly. Whenever I run the code and I get an error for too many requests, it continues displaying this instead of stopping and following the "except" block. Has this changed since you've posted this?

Please let me know if you have any information on this.

I know this is old, but just in case the "statusCode": 429, "reason": "Too Many Requests", "content": {"errorCode": 4003, "message": "Rate limit exceeded." error is setup to auto retry in the SDK so it is really an error. The SDK waits and resends, so no reason to trap that error in an exception.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment