I am trying to compare the output of a clienterror message to a string in python, but I can't get it to work. Is this kind of thing possible ?
I have tried 3 ways to solve it: (A) (I get no output)
except ClientError as e:
a = "An error occurred (ValidationError) when calling the UpdateStack operation: Stack [name] does not exist"
b = "An error occurred (ValidationError) when calling the UpdateStack operation: No updates are to be performed."
if e is a:
print ('Stack [name] does not exist')
if e == b:
print ('No updates are to be performed')
(B)
print e[5]
IndexError: tuple index out of range
--
print (e[76:])
()
print (e[:-1])
()
(C) I'm trying to understand how many parts are to the sentence, by running
print (len(e))
But I get "TypeError: object of type 'ClientError' has no len()"
So... can't I really compare the ClientErrorto a string ?
No, you can't compare a string to an exception. They aren't the same type.
iswill only return true if they are literally the same object. Even==wouldn't work here since as mentioned, they aren't the same type.You can however cast the exception to a string, then use
into see if it contains the message:The
incheck will only work however ifstr(e)includes the full error message. If it doesn't, you'll need to get the message another way. How exception messages are handled depends on the subclass, and I don't have access to aClientErrorobject to test; nor can I find a definition of it anywhere online.