Skip to content

Instantly share code, notes, and snippets.

@hidva
Last active May 6, 2017 05:55
Show Gist options
  • Save hidva/1cb767cfbb45b1619bfdc94513ee97c5 to your computer and use it in GitHub Desktop.
Save hidva/1cb767cfbb45b1619bfdc94513ee97c5 to your computer and use it in GitHub Desktop.
WrapperException
# coding=utf-8
import traceback
import sys
import logging
logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger('test')
class WrapperException(Exception):
"""
WrapperException.
"""
def __init__(self, cause, *args):
"""
:type cause: tuple
:param cause: 其格式语义与 sys.exc_info() 一致.
:param args: 其语义与 Exception() 的构造函数一致. 用来存放额外的信息.
"""
Exception.__init__(self, *args)
self.cause = cause
def __str__(self):
exc_str = Exception.__str__(self)
exc_str += ' CAUSED BY: \n'
for msgstr in traceback.format_exception(*self.cause):
exc_str += msgstr
return exc_str
def h1():
raise Exception(1, 2, 3)
def h2():
h1()
def h3():
try:
h2()
except:
raise WrapperException(sys.exc_info(), 9, 9, 9)
def h4():
h3()
def h5():
try:
h4()
except:
raise WrapperException(sys.exc_info(), 0x66, 0xcc, 0xff)
def h6():
h5()
def h7():
try:
h6()
except:
logger.exception('i got you')
if __name__ == '__main__':
h7()
ERROR:test:i got you
Traceback (most recent call last):
File "main.py", line 64, in h7
h6()
File "main.py", line 59, in h6
h5()
File "main.py", line 55, in h5
raise WrapperException(sys.exc_info(), 0x66, 0xcc, 0xff)
WrapperException: (102, 204, 255) CAUSED BY:
Traceback (most recent call last):
File "main.py", line 53, in h5
h4()
File "main.py", line 48, in h4
h3()
File "main.py", line 44, in h3
raise WrapperException(sys.exc_info(), 9, 9, 9)
WrapperException: (9, 9, 9) CAUSED BY:
Traceback (most recent call last):
File "main.py", line 42, in h3
h2()
File "main.py", line 37, in h2
h1()
File "main.py", line 33, in h1
raise Exception(1, 2, 3)
Exception: (1, 2, 3)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment