blob: 16b742d53606aec118c8e4b1a35e946ed52490ad [file] [log] [blame]
mbligh906b9f72007-11-29 18:56:17 +00001"""
2Internal global error types
3"""
4
jadmanski6f731362008-06-17 16:47:06 +00005import sys, traceback
mbligh906b9f72007-11-29 18:56:17 +00006from traceback import format_exception
7
mbligh91672252008-10-16 22:28:34 +00008# Add names you want to be imported by 'from errors import *' to this list.
9# This must be list not a tuple as we modify it to include all of our
10# the Exception classes we define below at the end of this file.
11__all__ = ['format_error']
12
13
mbligh906b9f72007-11-29 18:56:17 +000014def format_error():
jadmanski0afbb632008-06-06 21:10:57 +000015 t, o, tb = sys.exc_info()
16 trace = format_exception(t, o, tb)
17 # Clear the backtrace to prevent a circular reference
18 # in the heap -- as per tutorial
19 tb = ''
mbligh906b9f72007-11-29 18:56:17 +000020
jadmanski0afbb632008-06-06 21:10:57 +000021 return ''.join(trace)
mbligh906b9f72007-11-29 18:56:17 +000022
23class JobContinue(SystemExit):
jadmanski0afbb632008-06-06 21:10:57 +000024 """Allow us to bail out requesting continuance."""
25 pass
mbligh906b9f72007-11-29 18:56:17 +000026
mbligh7e1b1502008-06-06 15:05:41 +000027
mbligh906b9f72007-11-29 18:56:17 +000028class JobComplete(SystemExit):
jadmanski0afbb632008-06-06 21:10:57 +000029 """Allow us to bail out indicating continuation not required."""
30 pass
mbligh906b9f72007-11-29 18:56:17 +000031
mbligh7e1b1502008-06-06 15:05:41 +000032
mbligh906b9f72007-11-29 18:56:17 +000033class AutotestError(Exception):
jadmanski0afbb632008-06-06 21:10:57 +000034 """The parent of all errors deliberatly thrown within the client code."""
35 pass
mbligh906b9f72007-11-29 18:56:17 +000036
mbligh7e1b1502008-06-06 15:05:41 +000037
mbligh906b9f72007-11-29 18:56:17 +000038class JobError(AutotestError):
jadmanski0afbb632008-06-06 21:10:57 +000039 """Indicates an error which terminates and fails the whole job."""
40 pass
mbligh906b9f72007-11-29 18:56:17 +000041
mbligh7e1b1502008-06-06 15:05:41 +000042
mblighc2180832008-07-25 03:26:12 +000043class TestBaseException(AutotestError):
44 """The parent of all test exceptions."""
jadmanskif2171e22008-07-28 17:23:49 +000045 pass
mblighc2180832008-07-25 03:26:12 +000046
47
48class TestError(TestBaseException):
mblighb48fa562008-06-23 17:29:40 +000049 """Indicates that something went wrong with the test harness itself."""
50 exit_status="ERROR"
51 pass
52
jadmanski8d01bfe2008-06-23 18:13:24 +000053
mblighc2180832008-07-25 03:26:12 +000054class TestNAError(TestBaseException):
jadmanski0afbb632008-06-06 21:10:57 +000055 """Indictates that the test is Not Applicable. Should be thrown
mblighb48fa562008-06-23 17:29:40 +000056 when various conditions are such that the test is inappropriate."""
57 exit_status="TEST_NA"
58 pass
59
jadmanski8d01bfe2008-06-23 18:13:24 +000060
mblighc2180832008-07-25 03:26:12 +000061class TestFail(TestBaseException):
mblighb48fa562008-06-23 17:29:40 +000062 """Indicates that the test failed, but the job will not continue."""
63 exit_status="FAIL"
64 pass
65
jadmanski8d01bfe2008-06-23 18:13:24 +000066
mblighc2180832008-07-25 03:26:12 +000067class TestWarn(TestBaseException):
mblighb48fa562008-06-23 17:29:40 +000068 """Indicates that bad things (may) have happened, but not an explicit
69 failure."""
70 exit_status="WARN"
jadmanski0afbb632008-06-06 21:10:57 +000071 pass
mbligh6a2a2df2008-01-16 17:41:55 +000072
mbligh7e1b1502008-06-06 15:05:41 +000073
mblighc2180832008-07-25 03:26:12 +000074class UnhandledTestError(TestError):
75 """Indicates an unhandled error in a test."""
76 def __init__(self, unhandled_exception):
77 if isinstance(unhandled_exception, TestError):
78 TestError.__init__(self, *unhandled_exception.args)
79 else:
80 msg = "Unhandled %s: %s"
81 msg %= (unhandled_exception.__class__.__name__,
82 unhandled_exception)
83 msg += "\n" + traceback.format_exc()
84 TestError.__init__(self, msg)
85
86
87class UnhandledTestFail(TestFail):
88 """Indicates an unhandled fail in a test."""
89 def __init__(self, unhandled_exception):
90 if isinstance(unhandled_exception, TestFail):
91 TestFail.__init__(self, *unhandled_exception.args)
92 else:
93 msg = "Unhandled %s: %s"
94 msg %= (unhandled_exception.__class__.__name__,
95 unhandled_exception)
96 msg += "\n" + traceback.format_exc()
97 TestFail.__init__(self, msg)
98
99
mbligh906b9f72007-11-29 18:56:17 +0000100class CmdError(TestError):
jadmanski0afbb632008-06-06 21:10:57 +0000101 """\
102 Indicates that a command failed, is fatal to the test unless caught.
103 """
104 def __init__(self, command, result_obj, additional_text=None):
105 TestError.__init__(self, command, result_obj, additional_text)
mblighc23051c2008-06-27 19:26:46 +0000106 self.command = command
107 self.result_obj = result_obj
108 self.additional_text = additional_text
mbligh6a2a2df2008-01-16 17:41:55 +0000109
110
jadmanski0afbb632008-06-06 21:10:57 +0000111 def __str__(self):
jadmanski6ef0b672008-09-30 22:50:19 +0000112 if self.result_obj.exit_status is None:
113 msg = "Command <%s> failed and is not responding to signals"
114 msg %= self.command
115 else:
116 msg = "Command <%s> failed, rc=%d"
117 msg %= (self.command, self.result_obj.exit_status)
118
mblighc23051c2008-06-27 19:26:46 +0000119 if self.additional_text:
120 msg += ", " + self.additional_text
showard6d7e94f2008-08-20 20:53:34 +0000121 msg += '\n' + repr(self.result_obj)
jadmanski0afbb632008-06-06 21:10:57 +0000122 return msg
mbligh906b9f72007-11-29 18:56:17 +0000123
mbligh7e1b1502008-06-06 15:05:41 +0000124
mbligh906b9f72007-11-29 18:56:17 +0000125class PackageError(TestError):
jadmanski0afbb632008-06-06 21:10:57 +0000126 """Indicates an error trying to perform a package operation."""
127 pass
mbligh906b9f72007-11-29 18:56:17 +0000128
mbligh7e1b1502008-06-06 15:05:41 +0000129
mblighe8673102008-07-16 14:09:03 +0000130class BarrierError(JobError):
131 """Indicates an error happened during a barrier operation."""
132 pass
133
134
mbligh5deff3d2008-01-04 21:21:28 +0000135class InstallError(JobError):
jadmanski0afbb632008-06-06 21:10:57 +0000136 """Indicates an installation error which Terminates and fails the job."""
137 pass
mbligh03f4fc72007-11-29 20:56:14 +0000138
mbligh7e1b1502008-06-06 15:05:41 +0000139
mbligh6f015c42008-02-12 20:55:03 +0000140class AutotestRunError(AutotestError):
jadmanski0afbb632008-06-06 21:10:57 +0000141 pass
mbligh6f015c42008-02-12 20:55:03 +0000142
mbligh7e1b1502008-06-06 15:05:41 +0000143
mbligh6f015c42008-02-12 20:55:03 +0000144class AutotestTimeoutError(AutotestError):
jadmanski0afbb632008-06-06 21:10:57 +0000145 """This exception is raised when an autotest test exceeds the timeout
146 parameter passed to run_timed_test and is killed.
147 """
mbligh6f015c42008-02-12 20:55:03 +0000148
149
mbligh03f4fc72007-11-29 20:56:14 +0000150# server-specific errors
151
152class AutoservError(Exception):
jadmanski0afbb632008-06-06 21:10:57 +0000153 pass
mbligh03f4fc72007-11-29 20:56:14 +0000154
155
mbligh34faa282008-01-16 17:44:49 +0000156class AutoservSSHTimeout(AutoservError):
jadmanski0afbb632008-06-06 21:10:57 +0000157 """SSH experienced a connection timeout"""
158 pass
mbligh34faa282008-01-16 17:44:49 +0000159
160
mbligh03f4fc72007-11-29 20:56:14 +0000161class AutoservRunError(AutoservError):
jadmanski0afbb632008-06-06 21:10:57 +0000162 """\
163 Errors raised by one of the run functions. Should always be
164 constructed with a tuple of two args (error description (str),
165 run result object).
166 """
167 def __init__(self, description, result_obj):
showard6d7e94f2008-08-20 20:53:34 +0000168 self.description = description
169 self.result_obj = result_obj
jadmanski0afbb632008-06-06 21:10:57 +0000170 AutoservError.__init__(self, description, result_obj)
mbligh03f4fc72007-11-29 20:56:14 +0000171
showard6d7e94f2008-08-20 20:53:34 +0000172 def __str__(self):
173 return self.description + '\n' + repr(self.result_obj)
174
mbligh03f4fc72007-11-29 20:56:14 +0000175
176class AutoservVirtError(AutoservError):
jadmanski0afbb632008-06-06 21:10:57 +0000177 """Vitualization related error"""
178 pass
mbligh03f4fc72007-11-29 20:56:14 +0000179
180
181class AutoservUnsupportedError(AutoservError):
jadmanski0afbb632008-06-06 21:10:57 +0000182 """Error raised when you try to use an unsupported optional feature"""
183 pass
mbligh03f4fc72007-11-29 20:56:14 +0000184
mbligh7e1b1502008-06-06 15:05:41 +0000185
mbligh03f4fc72007-11-29 20:56:14 +0000186class AutoservHostError(AutoservError):
jadmanski0afbb632008-06-06 21:10:57 +0000187 """Error reaching a host"""
188 pass
mbligh03f4fc72007-11-29 20:56:14 +0000189
mbligh7e1b1502008-06-06 15:05:41 +0000190
mbligh03f4fc72007-11-29 20:56:14 +0000191class AutoservRebootError(AutoservError):
jadmanski0afbb632008-06-06 21:10:57 +0000192 """Error occured while rebooting a machine"""
193 pass
mbligh6e2ffec2008-03-05 16:08:34 +0000194
mbligh7e1b1502008-06-06 15:05:41 +0000195
mbligh6e2ffec2008-03-05 16:08:34 +0000196class AutoservSubcommandError(AutoservError):
jadmanski0afbb632008-06-06 21:10:57 +0000197 """Indicates an error while executing a (forked) subcommand"""
198 def __init__(self, func, exit_code):
199 AutoservError.__init__(self, func, exit_code)
200 self.func = func
201 self.exit_code = exit_code
mbligh7e1b1502008-06-06 15:05:41 +0000202
jadmanski0afbb632008-06-06 21:10:57 +0000203 def __str__(self):
204 return ("Subcommand %s failed with exit code %d" %
205 (self.func, self.exit_code))
mbligh91672252008-10-16 22:28:34 +0000206
207
208# This MUST remain at the end of the file.
209# Limit 'from error import *' to only import the exception instances.
210for _name, _thing in locals().items():
211 try:
212 if issubclass(_thing, Exception):
213 __all__.append(_name)
214 except TypeError:
215 pass # _thing not a class
216__all__ = tuple(__all__)