How to catch errors (Smartsheet API Python SDK) - python-3.x

I am missing fundamental knowledge.
How to 'properly' catch errors returned by the API.
I'm using Python 3.+
If I pass in the wrong sheet ID
try:
update_sht = SmSh.Sheets.get_sheet(dd_id)
print("OK?:", update_sht, flush=True)
except:
print("Error Print:\n", update_sht)
I get this response (in the IPython console)
Response: {
status: 404 Not Found
content: {
{
"errorCode": 1006,
"message": "Not Found",
"refId": "jod4cgoou0sw"
}
}
OK?: {"result": {"code": 1006, "errorCode": 1006, "message": "Not Found",
"name": "ApiError", "recommendation": "Do not retry without fixing the
problem. ", "refId": "jod4cgoou0sw", "shouldRetry": false,
"statusCode": 404}}
and while it returns an error response, it isn't an exception according to try/except.
At this point, I would like to exit out of the loop I am in, instead of continuing on until I get to other lines of code like this
for col in update_sht.columns:
that DO give errors that cause the program to fail.
Traceback (most recent call last):
File "<ipython-input-195-85dde6ec7071>", line 1, in <module>
xxx(debug=False)
File "<ipython-input-194-0b889c817b08>", line 75, in xxx
for col in update_sht.columns:
AttributeError: 'Error' object has no attribute 'columns'
I'm doing more than one thing on the sheet, if I find it, and would prefer not to have a try/except around error line of code (I exaggerate) unless I need them for other errors.
I know/hope this is easier than I have been trying to make it, but as I opened with, I am missing something fundamental.
-Craig
---- UPDATE ----
I am going around in circles.
If errors_as_exceptions is true, then this
update_sht = SmSh.Sheets.get_sheet(dd_id)
raises and exception, but
print(update_sht)
or anything similar shows the previous good value in the object.
How do I determine the status code and error codes so I can take appropriate action?
Nothing I have tried has worked.
If errors_as_exceptions is false, then this
update_sht = SmSh.Sheets.get_sheet(dd_id)
print(update_sht.result.code)
gives me the error code, but only when there is an error ... so I need to catch the error that occurs when there is no
error.
If I raise the error (errors_as_exceptions=True), how do I determine the status code and error code and if I don't raise the error,
how do I do the same?
I want to prevent my code from failing and give the user useful information on what needs to be fixed.

If you set ss.errors_as_exceptions() and your code looks something like this
try:
my_sheet = ss.Sheets.get_sheet(sheet_ID)
print(my_sheet)
except Exception as e:
print(e.message)
The result will look something like this 1006: Not Found. So, the exception message appears to be errorCode:message.

If you want the Python SDK to raise exceptions for API errors call the errors_as_exceptions method on the client object, e.g.
ss = smartsheet.Smartsheet()
ss.errors_as_exceptions()

Related

Python string substitution picking up junk characters in api requests

I am using Pagerduty python api client - pdpyras And below is the block of code
session = APISession(api_token)
changedate = os.popen("date -u -d '24 hours ago' +'%FT%T%.2Z'").read()
print(changedate)
changeurl = "change_events?include[]=integration&since=" + changedate
print(changeurl)
change_dump = session.rget(changeurl)
print(change_dump[0])
This should ideally get me the 1st event from change-events page of pagerduty. However, it fails due to the junk characters that gets added in the changeurl when it is passed to session.rget
See the below output
2021-03-26T09:47:53.2Z
change_events?include[]=integration&since=2021-03-26T09:47:53.2Z
Traceback (most recent call last):
File "dailyincidents.py", line 28, in <module>
change_dump = session.rget(changeurl)
File "/Users/saha/.pyenv/versions/3.7.7/lib/python3.7/site-packages/pdpyras.py", line 190, in call
return method(self, url, **kw)
File "/Users/saha/.pyenv/versions/3.7.7/lib/python3.7/site-packages/pdpyras.py", line 143, in call
r = raise_on_error(method(self, path, **pass_kw))
File "/Users/saha/.pyenv/versions/3.7.7/lib/python3.7/site-packages/pdpyras.py", line 84, in raise_on_error
), response=r
pdpyras.PDClientError: GET /change_events?include%5B%5D=integration&since=2021-03-26T09:47:53.2Z%0A: API responded with non-success status (400)
The problem here is the junk characters you see in the last line of error "include%5B%5D" and then "%0A" in the end. Because if I run the below code block directly, I am able to successfully pull the details.
change_dump = session.rget("change_events?include[]=integration&since=2021-03-26T09:47:53.2Z")
The issue occurs only when the string gets substituted, it takes these junk characters. Not sure how it gets picked and how to get around this. Any pointers?
EDIT
I was able to partly get rid of the junk characters. However, there is still one at the end, which I am not sure how to. Below is the change I did.
parameters = "include[]=integration&since=" + changedate
change_dump = session.rget("change_events", params=parameters)
Now, the error is like below
pdpyras.PDClientError: GET
/change_events?include[]=integration&since=2021-03-26T14:14:08.2Z%0A:
API responded with non-success status (400)
As you can see, now the last 3 characters in the url which is %0A: is causing the problem. Any pointers please?
The below format should work for you.
parameters = "include[]=integration&since=" + changedate
change_dump = session.rget("change_events",params=parameters.rstrip('\n').replace("\n", ""))

Strange issue with Python recursive function in Chalice framework

I have defined this SNS-triggered Lambda in Chalice:
#app.on_sns_message(topic='arn:aws:sns:us-west-1:XXXXXXXX:MyTopic')
def step1_photo_url_preload(event, retry = 3):
try:
js = json.loads(event.message)
... some logic here, event object is never modified ...
except:
if retry:
print("WARNING: failed, %d retries remaining" % retry)
return step1_photo_url_preload(event, retry-1)
else:
raise
When an exception is raised, the function should retry up to 3 times.
Instead, what I get is the exception below. Look closely at the trace: Line 56 shows the error occurs when attempting the recursive call:
[ERROR] TypeError: 'SNSEvent' object is not subscriptable
Traceback (most recent call last):
File "/var/task/chalice/app.py", line 1459, in __call__
return self.func(event_obj)
File "/var/task/app.py", line 56, in step1_photo_url_preload
return step1_photo_url_preload(event, retry-1)
File "/var/task/chalice/app.py", line 1458, in __call__
event_obj = self.event_class(event, context)
File "/var/task/chalice/app.py", line 1486, in __init__
self._extract_attributes(event_dict)
File "/var/task/chalice/app.py", line 1532, in _extract_attributes
first_record = event_dict['Records'][0]
Mysteriously, the function can't work with the event object that it received the first time.
What could cause this?
I suspect this might have something to do with the magic behind #app.on_sns_message, but I'm not sure where to look next.
The problem is the fact that the function is decorated, the failure is in the code the decorator is running. Pull the functionality you want to run recursively into a separate function and the problem should go away.

Trouble sending a batch create entity request in dialogflow

I have defined the following function. The purpose is to make batch create entity request with dialogflow client. I am using this method after sending many individual tests did not scale well.
The problem seems to be the line that defines EntityType. Seems like "entityType" is not valid but that is what is in the dialogflow v2 documentation which is the current version I am using.
Any ideas on what the issue is?
def create_batch_entity_types(self):
client = self.get_entity_client()
print(DialogFlowClient.batch_list)
EntityType = {
"entityTypes": DialogFlowClient.batch_list
}
response = client.batch_update_entity_types(parent=AGENT_PATH, entity_type_batch_inline=EntityType)
def callback(operation_future):
# Handle result.
result = operation_future.result()
print(result)
response.add_done_callback(callback)
After running the function I received this error
Traceback (most recent call last):
File "df_client.py", line 540, in <module>
create_entity_types_from_database()
File "df_client.py", line 426, in create_entity_types_from_database
df.create_batch_entity_types()
File "/Users/andrewflorial/Documents/PROJECTS/curlbot/dialogflow/dialogflow_accessor.py", line 99, in create_batch_entity_types
response = client.batch_update_entity_types(parent=AGENT_PATH, entity_type_batch_inline=EntityType)
File "/Users/andrewflorial/Documents/PROJECTS/curlbot/venv/lib/python3.7/site-packages/dialogflow_v2/gapic/entity_types_client.py", line 767, in batch_update_entity_types
update_mask=update_mask,
ValueError: Protocol message EntityTypeBatch has no "entityTypes" field.
The argument for entity_type_batch_inline must have the same form as EntityTypeBatch.
Look how that type looks like: https://dialogflow-python-client-v2.readthedocs.io/en/latest/gapic/v2/types.html#dialogflow_v2.types.EntityTypeBatch
It has to have entity_types field, not entityTypes.

How do I capture all error & display an error message

In my python code, I would like to catch all the errors & display an error message. For example, I would like to do this thing
try:
'my code block
catch:
print("Error:x error occurred" )
Can you suggest me how to do this?
If you're interested in the exception type, then you can catch all exceptions using except Exception as ex (ex can be anything), and then get the exception type using type(ex).__name__:
try:
# example, dividing by zero
x = 1 / 0
except Exception as ex:
print("Error: {} error occurred".format(type(ex).__name__))
Output:
Error: ZeroDivisionError error occurred
If the type doesn't matter, then this will do:
try:
# some code
except:
print("Error:an error occurred") # any error, but you don't know which

python logging.critical() to raise exception and dump stacktrace and die

I'm porting some code from perl (log4perl) and java (slf4j). All is fine except for logging.critical() does not dump stacktrace and die like it does in the other frameworks, need to add a lot of extra code, logger.exception() also only writes error.
Today I do:
try:
errmsg = "--id={} not found on --host={}".format(args.siteid, args.host)
raise GX8Exception(errmsg)
except GX8Exception as e:
log.exception(e)
sys.exit(-1)
This produces:
2018-01-10 10:09:56,814 [ERROR ] root --id=7A4A7845-7559-4F89-B678-8ADFECF5F7C3 not found on --host=welfare-qa
Traceback (most recent call last):
File "./gx8-controller.py", line 85, in <module>
raise GX8Exception(errmsg)
GX8Exception: --id=7A4A7845-7559-4F89-B678-8ADFECF5F7C3 not found on --host=welfare-qa
Is there a way to config pythonmodule logger to do this, or any other framework to do the same:
log.critical("--id={} not found on --host={}".format(args.siteid, args.host))
One approach would be to create a custom Handler that does nothing but pass log messages on to its super and then exit if the log level is high enough:
import logging
class ExitOnExceptionHandler(logging.StreamHandler):
def emit(self, record):
super().emit(record)
if record.levelno in (logging.ERROR, logging.CRITICAL):
raise SystemExit(-1)
logging.basicConfig(handlers=[ExitOnExceptionHandler()], level=logging.DEBUG)
logger = logging.getLogger('MYTHING')
def causeAProblem():
try:
raise ValueError("Oh no!")
except Exception as e:
logger.exception(e)
logger.warning('Going to try something risky...')
causeAProblem()
print("This won't get printed")
Output:
rat#pandion:~$ python test.py
ERROR:root:Oh no!
Traceback (most recent call last):
File "test.py", line 14, in causeAProblem
raise ValueError("Oh no!")
ValueError: Oh no!
rat#pandion:~$ echo $?
255
However, this could cause unexpected behavior for users of your code. It would be much more straightfoward, if you want to log an exception and exit, to simply leave the exception uncaught. If you want to log a traceback and exit wherever the code is currently calling logging.critical, change it to raise an exception instead.
I inherited some code where I could not change the handler class. I resorted to run time patching of the handler which is a variation on the solution by #nathan-vērzemnieks:
import types
def patch_logging_handler(logger):
def custom_emit(self, record):
self.orig_emit(record)
if record.levelno == logging.FATAL:
raise SystemExit(-1)
handler = logger.handlers[0]
setattr(handler, 'orig_emit', handler.emit)
setattr(handler, 'emit', types.MethodType(custom_emit, handler))
Nathans anwser is great! Been looking for this for a long time,
will just add that you can also do:
if record.levelno >= logging.ERROR:
instead of
if record.levelno in (logging.ERROR, logging.CRITICAL):
to set the minimum level that would cause an exit.

Resources