error.py revision 6ef0b672180d334b4c992315dc63ee4e2d3c9128
1""" 2Internal global error types 3""" 4 5import sys, traceback 6from traceback import format_exception 7 8def format_error(): 9 t, o, tb = sys.exc_info() 10 trace = format_exception(t, o, tb) 11 # Clear the backtrace to prevent a circular reference 12 # in the heap -- as per tutorial 13 tb = '' 14 15 return ''.join(trace) 16 17class JobContinue(SystemExit): 18 """Allow us to bail out requesting continuance.""" 19 pass 20 21 22class JobComplete(SystemExit): 23 """Allow us to bail out indicating continuation not required.""" 24 pass 25 26 27class AutotestError(Exception): 28 """The parent of all errors deliberatly thrown within the client code.""" 29 pass 30 31 32class JobError(AutotestError): 33 """Indicates an error which terminates and fails the whole job.""" 34 pass 35 36 37class TestBaseException(AutotestError): 38 """The parent of all test exceptions.""" 39 pass 40 41 42class TestError(TestBaseException): 43 """Indicates that something went wrong with the test harness itself.""" 44 exit_status="ERROR" 45 pass 46 47 48class TestNAError(TestBaseException): 49 """Indictates that the test is Not Applicable. Should be thrown 50 when various conditions are such that the test is inappropriate.""" 51 exit_status="TEST_NA" 52 pass 53 54 55class TestFail(TestBaseException): 56 """Indicates that the test failed, but the job will not continue.""" 57 exit_status="FAIL" 58 pass 59 60 61class TestWarn(TestBaseException): 62 """Indicates that bad things (may) have happened, but not an explicit 63 failure.""" 64 exit_status="WARN" 65 pass 66 67 68class UnhandledTestError(TestError): 69 """Indicates an unhandled error in a test.""" 70 def __init__(self, unhandled_exception): 71 if isinstance(unhandled_exception, TestError): 72 TestError.__init__(self, *unhandled_exception.args) 73 else: 74 msg = "Unhandled %s: %s" 75 msg %= (unhandled_exception.__class__.__name__, 76 unhandled_exception) 77 msg += "\n" + traceback.format_exc() 78 TestError.__init__(self, msg) 79 80 81class UnhandledTestFail(TestFail): 82 """Indicates an unhandled fail in a test.""" 83 def __init__(self, unhandled_exception): 84 if isinstance(unhandled_exception, TestFail): 85 TestFail.__init__(self, *unhandled_exception.args) 86 else: 87 msg = "Unhandled %s: %s" 88 msg %= (unhandled_exception.__class__.__name__, 89 unhandled_exception) 90 msg += "\n" + traceback.format_exc() 91 TestFail.__init__(self, msg) 92 93 94class CmdError(TestError): 95 """\ 96 Indicates that a command failed, is fatal to the test unless caught. 97 """ 98 def __init__(self, command, result_obj, additional_text=None): 99 TestError.__init__(self, command, result_obj, additional_text) 100 self.command = command 101 self.result_obj = result_obj 102 self.additional_text = additional_text 103 104 105 def __str__(self): 106 if self.result_obj.exit_status is None: 107 msg = "Command <%s> failed and is not responding to signals" 108 msg %= self.command 109 else: 110 msg = "Command <%s> failed, rc=%d" 111 msg %= (self.command, self.result_obj.exit_status) 112 113 if self.additional_text: 114 msg += ", " + self.additional_text 115 msg += '\n' + repr(self.result_obj) 116 return msg 117 118 119class PackageError(TestError): 120 """Indicates an error trying to perform a package operation.""" 121 pass 122 123 124class BarrierError(JobError): 125 """Indicates an error happened during a barrier operation.""" 126 pass 127 128 129class InstallError(JobError): 130 """Indicates an installation error which Terminates and fails the job.""" 131 pass 132 133 134class AutotestRunError(AutotestError): 135 pass 136 137 138class AutotestTimeoutError(AutotestError): 139 """This exception is raised when an autotest test exceeds the timeout 140 parameter passed to run_timed_test and is killed. 141 """ 142 143 144# server-specific errors 145 146class AutoservError(Exception): 147 pass 148 149 150class AutoservSSHTimeout(AutoservError): 151 """SSH experienced a connection timeout""" 152 pass 153 154 155class AutoservRunError(AutoservError): 156 """\ 157 Errors raised by one of the run functions. Should always be 158 constructed with a tuple of two args (error description (str), 159 run result object). 160 """ 161 def __init__(self, description, result_obj): 162 self.description = description 163 self.result_obj = result_obj 164 AutoservError.__init__(self, description, result_obj) 165 166 def __str__(self): 167 return self.description + '\n' + repr(self.result_obj) 168 169 170class AutoservVirtError(AutoservError): 171 """Vitualization related error""" 172 pass 173 174 175class AutoservUnsupportedError(AutoservError): 176 """Error raised when you try to use an unsupported optional feature""" 177 pass 178 179 180class AutoservHostError(AutoservError): 181 """Error reaching a host""" 182 pass 183 184 185class AutoservRebootError(AutoservError): 186 """Error occured while rebooting a machine""" 187 pass 188 189 190class AutoservSubcommandError(AutoservError): 191 """Indicates an error while executing a (forked) subcommand""" 192 def __init__(self, func, exit_code): 193 AutoservError.__init__(self, func, exit_code) 194 self.func = func 195 self.exit_code = exit_code 196 197 def __str__(self): 198 return ("Subcommand %s failed with exit code %d" % 199 (self.func, self.exit_code)) 200