Robot Framework
error.py
Go to the documentation of this file.
1 # Copyright 2008-2015 Nokia Networks
2 # Copyright 2016- Robot Framework Foundation
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
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.
15 
16 import os
17 import sys
18 import traceback
19 
20 from robot.errors import RobotError
21 
22 from .platform import RERAISED_EXCEPTIONS
23 
24 
25 EXCLUDE_ROBOT_TRACES = not os.getenv('ROBOT_INTERNAL_TRACES')
26 
27 
28 
35  return ErrorDetails().message
36 
37 
38 
39 def get_error_details(full_traceback=True, exclude_robot_traces=EXCLUDE_ROBOT_TRACES):
40  details = ErrorDetails(full_traceback=full_traceback,
41  exclude_robot_traces=exclude_robot_traces)
42  return details.message, details.traceback
43 
44 
45 
52 
55  _generic_names = frozenset(('AssertionError', 'Error', 'Exception', 'RuntimeError'))
56 
57  def __init__(self, error=None, full_traceback=True,
58  exclude_robot_traces=EXCLUDE_ROBOT_TRACES):
59  if not error:
60  error = sys.exc_info()[1]
61  if isinstance(error, RERAISED_EXCEPTIONS):
62  raise error
63  self.errorerror = error
64  self._full_traceback_full_traceback = full_traceback
65  self._exclude_robot_traces_exclude_robot_traces = exclude_robot_traces
66  self._message_message = None
67  self._traceback_traceback = None
68 
69  @property
70  message = property
71 
72  def message(self):
73  if self._message_message is None:
74  self._message_message = self._format_message_format_message(self.errorerror)
75  return self._message_message
76 
77  @property
78  traceback = property
79 
80  def traceback(self):
81  if self._traceback_traceback is None:
82  self._traceback_traceback = self._format_traceback_format_traceback(self.errorerror)
83  return self._traceback_traceback
84 
85  def _format_traceback(self, error):
86  if isinstance(error, RobotError):
87  return error.details
88  if self._exclude_robot_traces_exclude_robot_traces:
89  self._remove_robot_traces_remove_robot_traces(error)
90  lines = self._get_traceback_lines_get_traceback_lines(type(error), error, error.__traceback__)
91  return ''.join(lines).rstrip()
92 
93  def _remove_robot_traces(self, error):
94  tb = error.__traceback__
95  while tb and self._is_robot_traceback_is_robot_traceback(tb):
96  tb = tb.tb_next
97  error.__traceback__ = tb
98  if error.__context__:
99  self._remove_robot_traces_remove_robot_traces(error.__context__)
100  if error.__cause__:
101  self._remove_robot_traces_remove_robot_traces(error.__cause__)
102 
103  def _is_robot_traceback(self, tb):
104  module = tb.tb_frame.f_globals.get('__name__')
105  return module and module.startswith('robot.')
106 
107  def _get_traceback_lines(self, etype, value, tb):
108  prefix = 'Traceback (most recent call last):\n'
109  empty_tb = [prefix, ' None\n']
110  if self._full_traceback_full_traceback:
111  if tb or value.__context__ or value.__cause__:
112  return traceback.format_exception(etype, value, tb)
113  else:
114  return empty_tb + traceback.format_exception_only(etype, value)
115  else:
116  if tb:
117  return [prefix] + traceback.format_tb(tb)
118  else:
119  return empty_tb
120 
121  def _format_message(self, error):
122  name = type(error).__name__.split('.')[-1] # Use only the last part
123  message = str(error)
124  if not message:
125  return name
126  if self._suppress_name_suppress_name(name, error):
127  return message
128  if message.startswith('*HTML*'):
129  name = '*HTML* ' + name
130  message = message.split('*', 2)[-1].lstrip()
131  return '%s: %s' % (name, message)
132 
133  def _suppress_name(self, name, error):
134  return (name in self._generic_names_generic_names
135  or isinstance(error, RobotError)
136  or getattr(error, 'ROBOT_SUPPRESS_NAME', False))
Object wrapping the last occurred exception.
Definition: error.py:51
def __init__(self, error=None, full_traceback=True, exclude_robot_traces=EXCLUDE_ROBOT_TRACES)
Definition: error.py:58
def _format_traceback(self, error)
Definition: error.py:85
def _is_robot_traceback(self, tb)
Definition: error.py:103
def _format_message(self, error)
Definition: error.py:121
def _remove_robot_traces(self, error)
Definition: error.py:93
def _suppress_name(self, name, error)
Definition: error.py:133
def _get_traceback_lines(self, etype, value, tb)
Definition: error.py:107
def get_error_details(full_traceback=True, exclude_robot_traces=EXCLUDE_ROBOT_TRACES)
Returns error message and details of the last occurred exception.
Definition: error.py:39
def get_error_message()
Returns error message of the last occurred exception.
Definition: error.py:34