How to serialize an Exception

Viewed 15773

When I try to serialize an exception using json.dump, I get errors like

TypeError: IOError('socket error', error(61, 'Connection refused')) is not JSON serializable

and

TypeError: error(61, 'Connection refused') is not JSON serializable

The __dict__ field of exceptions is {} (this is why How to make a class JSON serializable does not help me: the answers there assume that __dict__ contains all the necessary information, they also assume that I have control over the class to be serialized).

Is there something more intelligent that saving str(exn)?

I would prefer a human-readable text representation (not pickle).

PS. Here is what I came up with:

def exception_as_dict(ex):
    return dict(type=ex.__class__.__name__,
                errno=ex.errno, message=ex.message,
                strerror=exception_as_dict(ex.strerror)
                if isinstance(ex.strerror,Exception) else ex.strerror)

json.dumps(exception_as_dict(err),indent=2)
{
  "errno": "socket error", 
  "type": "IOError", 
  "strerror": {
    "errno": 61, 
    "type": "error", 
    "strerror": "Connection refused"
  }
}
2 Answers

You can use exc_info with traceback as below:

import traceback
import sys
try:
    raise KeyError('aaa!!!')
except Exception as e:
    exc_info = sys.exc_info()
    print(''.join(traceback.format_exception(*exc_info)))
Related