Sergei Trofimov | 4e6afe9 | 2015-10-09 09:30:04 +0100 | [diff] [blame] | 1 | # Copyright 2013-2015 ARM Limited |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
| 14 | # |
| 15 | |
Sergei Trofimov | 4e6afe9 | 2015-10-09 09:30:04 +0100 | [diff] [blame] | 16 | class DevlibError(Exception): |
Brendan Jackman | 1fa6f92 | 2017-03-01 18:54:54 +0000 | [diff] [blame] | 17 | """Base class for all Devlib exceptions.""" |
Sergei Trofimov | 4e6afe9 | 2015-10-09 09:30:04 +0100 | [diff] [blame] | 18 | pass |
| 19 | |
| 20 | |
| 21 | class TargetError(DevlibError): |
| 22 | """An error has occured on the target""" |
| 23 | pass |
| 24 | |
| 25 | |
| 26 | class TargetNotRespondingError(DevlibError): |
| 27 | """The target is unresponsive.""" |
| 28 | |
| 29 | def __init__(self, target): |
| 30 | super(TargetNotRespondingError, self).__init__('Target {} is not responding.'.format(target)) |
| 31 | |
| 32 | |
| 33 | class HostError(DevlibError): |
| 34 | """An error has occured on the host""" |
| 35 | pass |
| 36 | |
Sergei Trofimov | a926503 | 2017-02-08 11:14:40 +0000 | [diff] [blame] | 37 | |
| 38 | class TimeoutError(DevlibError): |
| 39 | """Raised when a subprocess command times out. This is basically a ``DevlibError``-derived version |
| 40 | of ``subprocess.CalledProcessError``, the thinking being that while a timeout could be due to |
| 41 | programming error (e.g. not setting long enough timers), it is often due to some failure in the |
| 42 | environment, and there fore should be classed as a "user error".""" |
| 43 | |
| 44 | def __init__(self, command, output): |
| 45 | super(TimeoutError, self).__init__('Timed out: {}'.format(command)) |
| 46 | self.command = command |
| 47 | self.output = output |
| 48 | |
| 49 | def __str__(self): |
| 50 | return '\n'.join([self.message, 'OUTPUT:', self.output or '']) |
Sergei Trofimov | 8296d6c | 2017-06-06 10:24:49 +0100 | [diff] [blame] | 51 | |
| 52 | |
| 53 | class WorkerThreadError(DevlibError): |
| 54 | """ |
| 55 | This should get raised in the main thread if a non-WAError-derived |
| 56 | exception occurs on a worker/background thread. If a WAError-derived |
| 57 | exception is raised in the worker, then it that exception should be |
| 58 | re-raised on the main thread directly -- the main point of this is to |
| 59 | preserve the backtrace in the output, and backtrace doesn't get output for |
| 60 | WAErrors. |
| 61 | |
| 62 | """ |
| 63 | |
| 64 | def __init__(self, thread, exc_info): |
| 65 | self.thread = thread |
| 66 | self.exc_info = exc_info |
| 67 | orig = self.exc_info[1] |
| 68 | orig_name = type(orig).__name__ |
| 69 | message = 'Exception of type {} occured on thread {}:\n'.format(orig_name, thread) |
| 70 | message += '{}\n{}: {}'.format(get_traceback(self.exc_info), orig_name, orig) |
| 71 | super(WorkerThreadError, self).__init__(message) |
| 72 | |
| 73 | |
| 74 | def get_traceback(exc=None): |
| 75 | """ |
| 76 | Returns the string with the traceback for the specifiec exc |
| 77 | object, or for the current exception exc is not specified. |
| 78 | |
| 79 | """ |
| 80 | import StringIO, traceback, sys |
| 81 | if exc is None: |
| 82 | exc = sys.exc_info() |
| 83 | if not exc: |
| 84 | return None |
| 85 | tb = exc[2] |
| 86 | sio = StringIO.StringIO() |
| 87 | traceback.print_tb(tb, file=sio) |
| 88 | del tb # needs to be done explicitly see: http://docs.python.org/2/library/sys.html#sys.exc_info |
| 89 | return sio.getvalue() |