TimO February 2016

python find exception message (with ast?)

I want to compile a list of all messages my program could present to a user, specifically all errors (to be able to quality-check the wording). Let's assume I have my own exception class

class MyException( Exception ):
    pass

And I would want to find programmatically all instances where I

raise MyException( "This is a baadly worded message with typo." )

Is there an elegant python way to retrieve:

  • the message
  • the line number
  • the (member) function name that raises the exception?

Ideally, it would even work with cases like

msg = "My really long error message\n"
msg += "Where I explain too much to fit it directly into the raise call"
raise MyException( msg )

Answers


sorin February 2016

So you want to localize your Python application.

In this case have a loot at gettext as is a very powerful localization framework, one that is used by most open-source projects, not only Python ones.

Post Status

Asked in February 2016
Viewed 2,399 times
Voted 4
Answered 1 times

Search




Leave an answer