On Fri, May 12, 2006 at 10:23:43PM +0200, David Fraser wrote: > Hi > > Just wanted to post this snippet that I put in our code in case anyone else > finds it useful... > Basically if an error happens serializing the template, it looks in a > traceback > and gives you some kind of idea as to which point in the XML file the error > happened, by printing out the ancestors of the current XML element. > (This is from jToolkit, which is used amongst other things for Pootle, the > open > source translation web system) > > Cheers > David > > t = kid.Template(source=source,**context) > try: > return t.serialize() > except Exception, e: > tb = sys.exc_info()[2] > tb_point = tb > while tb_point.tb_next: > tb_point = tb_point.tb_next > ancestors = tb_point.tb_frame.f_locals.get("ancestors", []) > xml_traceback = [] > for ancestor in ancestors: > if ancestor is None: continue > try: > ancestor_str = kid.et.tostring(ancestor) > except Exception, e: > ancestor_str = "(could not convert %s: %s)" % (str(ancestor), > str(e)) > xml_traceback.append(ancestor_str) > context_str = pprint.pformat(context) > xml_traceback_str = " " + "\n ".join(xml_traceback) > self.errorhandler.logerror("Error converting template: > %s\nContext\n%s\nXML Ancestors:\n%s\n%s\n" % (e, context_str, > xml_traceback_str, self.errorhandler.traceback_str())) > if self.webserver.options.debug: > import pdb > pdb.post_mortem(tb) >
Very interesting. I am thinking that we could probably use this to hold us over until we can really get bettor error handling. Something simple like changing the parsing to catch exceptions caused by bad XML and raise a new exception. The new exception would be initialized with a string or list by executing the code above. Then users can 'except KidParseError e:' (or something like that) and call 'e.get_ancestors()' (or something like that). This should not be the final solution, but it is definitely a huge step forward. I'm sure if we can somehow work this in we'd have lots of happy users. David Stanek -- http://www.traceback.org GPG keyID #6272EDAF on http://pgp.mit.edu Key fingerprint = 8BAA 7E11 8856 E148 6833 655A 92E2 3E00 6272 EDAF
pgpZRLnhxSkfs.pgp
Description: PGP signature