type(ex) can be wrong for exceptions that are old style classes
try

ex.__class__

Neither case will check for subclasses of exceptions though. Not sure if that is intentional.

Maybe make message_map an instance of a dict subclass that uses isinstance() for it's key compares?