Wednesday 28 July 2010 — This is more than 14 years old. Be careful.
A simple piece of advice: If you are throwing an exception (or logging an error) about a value being incorrect in some way, include the value itself. It will make it so much easier for the poor sap who has to figure out why the exception is happening.
I found myself in this situation, this code throwing an exception:
if not isinstance(key, str):
raise Client.MemcachedStringEncodingError, ("Keys must be str()'s, not"
"unicode. Convert your unicode strings using "
"mystring.encode(charset)!")
There are a few things wrong with this message, the first being that the multi-line string concatenation is missing a space, so the message actually has the word “notunicode” in it. Why are we so sure the wrong value is Unicode in the first place? And of course, it should include the actual value:
if not isinstance(key, str):
raise Client.MemcachedStringEncodingError, (
"Keys must be str()'s: %r" % key
)
If you want to be paranoid, you can limit the amount of repr text that will appear in the message:
if not isinstance(key, str):
raise Client.MemcachedStringEncodingError, (
"Keys must be str()'s: %.60r" % key
)
If you are really paranoid, you’re worried that getting the repr of your unknown object could itself throw an exception:
def safe_repr(o):
try:
return repr(o)
except:
return "??norepr?"
...
if not isinstance(key, str):
raise Client.MemcachedStringEncodingError, (
"Keys must be str()'s: %.60s" % safe_repr(key)
)
or even:
def safe_repr(o):
try:
return repr(o)
except Exception, e:
return "??norepr (%s)?" % e
Good error handling is always a pain, but it’s worth it when things start hitting the fan and you have to figure out what’s going on.
Comments
Another trick I like to use quite often is to add values to exceptions other people raise that don't have enough info yet:
Add a comment: