Python 2.7.3
I got the following error when I ran SillyWalk.getGroups(user_id = user) for a user who, like the parrot, is no more.
Traceback (most recent call last):
File "C:\Python\API\getgroups.py", line 32, in <module>
Grp = SillyWalk.getGroups(user_id = user)
File "c:\python27\lib\site-packages\SillyWalkapi-1.4.2-py2.7.egg\SillyWalkapi\__init__.py", line 349, in handler
parse_format=args['format'], **args)
File "c:\python27\lib\site-packages\SillyWalkapi-1.4.2-py2.7.egg\SillyWalkapi\__init__.py", line 435, in __wrap_in_parser
return parser(self, data)
File "c:\python27\lib\site-packages\SillyWalkapi-1.4.2-py2.7.egg\SillyWalkapi\__init__.py", line 278, in parse_etree
raise SillyWalkError(u'Error: %(code)s: %(msg)s' % err.attrib)
SillyWalkError: Error: 1: User not found
I tried to fix the problem by putting a try ... except statement in:
try:
Grp = SillyWalk.getGroups(user_id = user)
g = 0
for group in Grp.find('groups').findall('group'):
g += 1
u_ID = user
g_ID = group.get('_ID')
gName = "" # group.get('name')
HasParrot = group.get('HasParrot')
Priv = group.get('Priv')
Mbr = group.get('members')
threads = group.get('threads')
tup = '{0}\t{1}\t{2}\t{3}\t{4}\t{5}\t{6}\n'.format(u_ID, g_ID, gName.encode('utf-8'), HasParrot, Priv, Mbr, threads)
OutFile.write(tup.encode('utf-8'))
except SillyWalkError:
tup = '{0}\tNo Records Found\n'.format(u_ID)
OutFile.write(tup.encode('utf-8'))
but it gave me an undefined error:
Traceback (most recent call last):
File "C:\Python\API\getgroups.py", line 47, in <module>
except SillyWalkError:
NameError: name 'SillyWalkError' is not defined
>>>
This is my first attempt to write an exception class, so could someone please show me how to do it? The SillyWalk.getGroups has a dozen or so different error numbers, for various error reasons. Do I need a case for each one?
I’ll be forever in your debt! (You’ll never collect anything from me, but I’ll still owe it to you!)
BTW: while rooting about in the guts of my script, could someone show me how to fix my gName problem, so I don’t need to null it out? It keeps giving me:
Traceback (most recent call last):
File "C:\Python\API\getgroups.py", line 44, in <module>
OutFile.write(tup.encode('utf-8'))
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 54: ordinal not in range(128)
NameError: name 'SillyWalkError' is not definedjust indicates that the nameSillyWalkErroris not defined. It’s nothing specific to exception handling or writing an exception class.It looks like you just haven’t imported the name
SillyWalkErrorinto the module that’s trying to catch the exception; the error you were getting without theexceptindicates that the exception class itself is working fine.