import traceback
import sys

try:
    from StringIO import StringIO
except:
    from io import StringIO


class InnerException(Exception):
    def __init__(self, inner_exception=None):
        self.inner_exception = inner_exception

        if self.inner_exception is not None:
            f = StringIO()
            exc_type, exc_value, exc_traceback = sys.exc_info()
            traceback.print_exception(exc_type, exc_value, exc_traceback, file=f)
            self.inner_stack_trace = f.getvalue()

    def __str__(self):
        s = super(InnerException, self).__str__()
        if self.inner_exception is not None:
            s += "\n"
            s += "Inner Exception:\n"
            s += self.inner_stack_trace
        return s
