I have custom exceptions in my django project that look like this:
class CustomFooError(Exception):
def __init__(self, msg="Something went wrong with Foo."):
self.msg = msg
def __str__(self):
return repr(self.msg)
At various points in my code I will raise exceptions like this:
raise CustomFooError("Things are going badly")
When Django catches these errors, in debug mode I get django’s standard pretty stack-trace page. But I never see my error messages — “Things are going badly” never shows up in the debug error page.
It seems they should show up as the Exception Value on the error page. I walked back through the django source far enough to find out that this is the value field from sys.exc_info() which is consistently tersely documented as “[the exception’s] associated value or the second argument to raise, which is always a class instance if the exception type is a class object.” Unfortunately, I don’t know what to do with this information.
So my question is: How should I be writing and raising my custom exceptions to get more useful data to show up in places like the django error screen?
I would just use
superand let the constructor ofExceptionhandle assigning themsgattribute:I just tested this from within a Django environment and it correctly displayed the message I passed to the exception constructor or the default one if None was passed.