blob: 8c002aa7a6e9eaed304348b85f8998fad6a8f988 [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."""
mbligh021679f2008-11-27 00:43:19 +000045 # Children are required to override this. Never instantiate directly.
46 exit_status="NEVER_RAISE_THIS"
mblighc2180832008-07-25 03:26:12 +000047
48
49class TestError(TestBaseException):
mblighb48fa562008-06-23 17:29:40 +000050 """Indicates that something went wrong with the test harness itself."""
51 exit_status="ERROR"
mblighb48fa562008-06-23 17:29:40 +000052
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"
mblighb48fa562008-06-23 17:29:40 +000058
jadmanski8d01bfe2008-06-23 18:13:24 +000059
mblighc2180832008-07-25 03:26:12 +000060class TestFail(TestBaseException):
mblighb48fa562008-06-23 17:29:40 +000061 """Indicates that the test failed, but the job will not continue."""
62 exit_status="FAIL"
mblighb48fa562008-06-23 17:29:40 +000063
jadmanski8d01bfe2008-06-23 18:13:24 +000064
mblighc2180832008-07-25 03:26:12 +000065class TestWarn(TestBaseException):
mblighb48fa562008-06-23 17:29:40 +000066 """Indicates that bad things (may) have happened, but not an explicit
67 failure."""
68 exit_status="WARN"
mbligh6a2a2df2008-01-16 17:41:55 +000069
mbligh7e1b1502008-06-06 15:05:41 +000070
mblighc2180832008-07-25 03:26:12 +000071class UnhandledTestError(TestError):
72 """Indicates an unhandled error in a test."""
73 def __init__(self, unhandled_exception):
74 if isinstance(unhandled_exception, TestError):
75 TestError.__init__(self, *unhandled_exception.args)
76 else:
77 msg = "Unhandled %s: %s"
78 msg %= (unhandled_exception.__class__.__name__,
79 unhandled_exception)
80 msg += "\n" + traceback.format_exc()
81 TestError.__init__(self, msg)
82
83
84class UnhandledTestFail(TestFail):
85 """Indicates an unhandled fail in a test."""
86 def __init__(self, unhandled_exception):
87 if isinstance(unhandled_exception, TestFail):
88 TestFail.__init__(self, *unhandled_exception.args)
89 else:
90 msg = "Unhandled %s: %s"
91 msg %= (unhandled_exception.__class__.__name__,
92 unhandled_exception)
93 msg += "\n" + traceback.format_exc()
94 TestFail.__init__(self, msg)
95
96
mbligh906b9f72007-11-29 18:56:17 +000097class CmdError(TestError):
jadmanski0afbb632008-06-06 21:10:57 +000098 """\
99 Indicates that a command failed, is fatal to the test unless caught.
100 """
101 def __init__(self, command, result_obj, additional_text=None):
102 TestError.__init__(self, command, result_obj, additional_text)
mblighc23051c2008-06-27 19:26:46 +0000103 self.command = command
104 self.result_obj = result_obj
105 self.additional_text = additional_text
mbligh6a2a2df2008-01-16 17:41:55 +0000106
107
jadmanski0afbb632008-06-06 21:10:57 +0000108 def __str__(self):
jadmanski6ef0b672008-09-30 22:50:19 +0000109 if self.result_obj.exit_status is None:
110 msg = "Command <%s> failed and is not responding to signals"
111 msg %= self.command
112 else:
113 msg = "Command <%s> failed, rc=%d"
114 msg %= (self.command, self.result_obj.exit_status)
115
mblighc23051c2008-06-27 19:26:46 +0000116 if self.additional_text:
117 msg += ", " + self.additional_text
showard6d7e94f2008-08-20 20:53:34 +0000118 msg += '\n' + repr(self.result_obj)
jadmanski0afbb632008-06-06 21:10:57 +0000119 return msg
mbligh906b9f72007-11-29 18:56:17 +0000120
mbligh7e1b1502008-06-06 15:05:41 +0000121
mbligh906b9f72007-11-29 18:56:17 +0000122class PackageError(TestError):
jadmanski0afbb632008-06-06 21:10:57 +0000123 """Indicates an error trying to perform a package operation."""
124 pass
mbligh906b9f72007-11-29 18:56:17 +0000125
mbligh7e1b1502008-06-06 15:05:41 +0000126
mblighe8673102008-07-16 14:09:03 +0000127class BarrierError(JobError):
128 """Indicates an error happened during a barrier operation."""
129 pass
130
131
mbligh5deff3d2008-01-04 21:21:28 +0000132class InstallError(JobError):
jadmanski0afbb632008-06-06 21:10:57 +0000133 """Indicates an installation error which Terminates and fails the job."""
134 pass
mbligh03f4fc72007-11-29 20:56:14 +0000135
mbligh7e1b1502008-06-06 15:05:41 +0000136
mbligh6f015c42008-02-12 20:55:03 +0000137class AutotestRunError(AutotestError):
mbligh021679f2008-11-27 00:43:19 +0000138 """Indicates a problem running server side control files."""
jadmanski0afbb632008-06-06 21:10:57 +0000139 pass
mbligh6f015c42008-02-12 20:55:03 +0000140
mbligh7e1b1502008-06-06 15:05:41 +0000141
mbligh6f015c42008-02-12 20:55:03 +0000142class AutotestTimeoutError(AutotestError):
jadmanski0afbb632008-06-06 21:10:57 +0000143 """This exception is raised when an autotest test exceeds the timeout
144 parameter passed to run_timed_test and is killed.
145 """
mbligh6f015c42008-02-12 20:55:03 +0000146
147
mbligh03f4fc72007-11-29 20:56:14 +0000148# server-specific errors
149
150class AutoservError(Exception):
jadmanski0afbb632008-06-06 21:10:57 +0000151 pass
mbligh03f4fc72007-11-29 20:56:14 +0000152
153
mbligh34faa282008-01-16 17:44:49 +0000154class AutoservSSHTimeout(AutoservError):
jadmanski0afbb632008-06-06 21:10:57 +0000155 """SSH experienced a connection timeout"""
156 pass
mbligh34faa282008-01-16 17:44:49 +0000157
158
mbligh03f4fc72007-11-29 20:56:14 +0000159class AutoservRunError(AutoservError):
jadmanski0afbb632008-06-06 21:10:57 +0000160 """\
161 Errors raised by one of the run functions. Should always be
162 constructed with a tuple of two args (error description (str),
163 run result object).
164 """
165 def __init__(self, description, result_obj):
showard6d7e94f2008-08-20 20:53:34 +0000166 self.description = description
167 self.result_obj = result_obj
jadmanski0afbb632008-06-06 21:10:57 +0000168 AutoservError.__init__(self, description, result_obj)
mbligh03f4fc72007-11-29 20:56:14 +0000169
showard6d7e94f2008-08-20 20:53:34 +0000170 def __str__(self):
171 return self.description + '\n' + repr(self.result_obj)
172
mbligh03f4fc72007-11-29 20:56:14 +0000173
174class AutoservVirtError(AutoservError):
jadmanski0afbb632008-06-06 21:10:57 +0000175 """Vitualization related error"""
176 pass
mbligh03f4fc72007-11-29 20:56:14 +0000177
178
179class AutoservUnsupportedError(AutoservError):
jadmanski0afbb632008-06-06 21:10:57 +0000180 """Error raised when you try to use an unsupported optional feature"""
181 pass
mbligh03f4fc72007-11-29 20:56:14 +0000182
mbligh7e1b1502008-06-06 15:05:41 +0000183
mbligh03f4fc72007-11-29 20:56:14 +0000184class AutoservHostError(AutoservError):
jadmanski0afbb632008-06-06 21:10:57 +0000185 """Error reaching a host"""
186 pass
mbligh03f4fc72007-11-29 20:56:14 +0000187
mbligh7e1b1502008-06-06 15:05:41 +0000188
mbligh03f4fc72007-11-29 20:56:14 +0000189class AutoservRebootError(AutoservError):
jadmanski0afbb632008-06-06 21:10:57 +0000190 """Error occured while rebooting a machine"""
191 pass
mbligh6e2ffec2008-03-05 16:08:34 +0000192
mbligh7e1b1502008-06-06 15:05:41 +0000193
mbligh6e2ffec2008-03-05 16:08:34 +0000194class AutoservSubcommandError(AutoservError):
jadmanski0afbb632008-06-06 21:10:57 +0000195 """Indicates an error while executing a (forked) subcommand"""
196 def __init__(self, func, exit_code):
197 AutoservError.__init__(self, func, exit_code)
198 self.func = func
199 self.exit_code = exit_code
mbligh7e1b1502008-06-06 15:05:41 +0000200
jadmanski0afbb632008-06-06 21:10:57 +0000201 def __str__(self):
202 return ("Subcommand %s failed with exit code %d" %
203 (self.func, self.exit_code))
mbligh91672252008-10-16 22:28:34 +0000204
205
206# This MUST remain at the end of the file.
207# Limit 'from error import *' to only import the exception instances.
208for _name, _thing in locals().items():
209 try:
210 if issubclass(_thing, Exception):
211 __all__.append(_name)
212 except TypeError:
213 pass # _thing not a class
214__all__ = tuple(__all__)