Last active
October 28, 2022 18:56
-
-
Save stmcallister/59560902ddd9b1abeb5056dee6a0f8f9 to your computer and use it in GitHub Desktop.
Accessing Smartsheet ApiError object in Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.