1 # Licensed to the Apache Software Foundation (ASF) under one or more
2 # contributor license agreements. See the NOTICE file distributed with
3 # this work for additional information regarding copyright ownership.
4 # The ASF licenses this file to You under the Apache License, Version 2.0
5 # (the "License"); you may not use this file except in compliance with
6 # the License. You may obtain a copy of the License at
8 # http://www.apache.org/licenses/LICENSE-2.0
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
17 Utilities for extracting and formatting Python exceptions.
23 import traceback as tb
27 from .console import (puts, indent, Colored)
30 ENTRY_FORMAT = 'File "{filename}", line {lineno}, in {name}'
33 def print_exception(e, full=True, cause=False, traceback=None):
35 Prints the exception with nice colors and such.
37 def format_heading(e):
38 return '{0}{1}: {2}'.format(
39 Colored.red('Caused by ') if cause else '',
40 Colored.red(e.__class__.__name__, bold=True),
43 puts(format_heading(e))
47 print_traceback(traceback, True)
50 if hasattr(e, 'cause') and e.cause:
51 traceback = e.cause_traceback if hasattr(e, 'cause_traceback') else None
52 print_exception(e.cause, full=full, cause=True, traceback=traceback)
55 def print_traceback(traceback=None, print_last_stack=False):
57 Prints the traceback with nice colors and such.
61 _, _, traceback = sys.exc_info()
62 while traceback is not None:
63 frame = traceback.tb_frame
65 filename = code.co_filename
66 lineno = traceback.tb_lineno
69 puts(ENTRY_FORMAT.format(filename=Colored.blue(filename),
70 lineno=Colored.cyan(lineno),
71 name=Colored.cyan(name)))
72 linecache.checkcache(filename)
73 line = linecache.getline(filename, lineno, frame.f_globals)
77 traceback = traceback.tb_next
78 if print_last_stack and (traceback is None):
79 # Print stack of *last* traceback
83 def _print_stack(frame):
84 entries = tb.extract_stack(frame)
87 puts(Colored.red('Call stack:'))
89 for filename, lineno, name, line in entries:
90 puts(ENTRY_FORMAT.format(filename=Colored.blue(filename),
91 lineno=Colored.cyan(lineno),
92 name=Colored.cyan(name)))
97 def get_exception_as_string(exc_type, exc_val, traceback):
98 s_traceback = StringIO.StringIO()
104 return s_traceback.getvalue()
107 class _WrappedException(Exception):
109 def __init__(self, exception_type, exception_str):
110 super(_WrappedException, self).__init__(exception_type, exception_str)
111 self.exception_type = exception_type
112 self.exception_str = exception_str
115 def wrap_if_needed(exception):
117 jsonpickle.loads(jsonpickle.dumps(exception))
119 except BaseException:
120 return _WrappedException(type(exception).__name__, str(exception))