usb_exceptions.py 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. # Copyright 2014 Google Inc. All rights reserved.
  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. """Common exceptions for ADB and Fastboot."""
  15. class CommonUsbError(Exception):
  16. """Base class for usb communication errors."""
  17. class FormatMessageWithArgumentsException(CommonUsbError):
  18. """Exception that both looks good and is functional.
  19. Okay, not that kind of functional, it's still a class.
  20. This interpolates the message with the given arguments to make it
  21. human-readable, but keeps the arguments in case other code try-excepts it.
  22. """
  23. def __init__(self, message, *args):
  24. message %= args
  25. super(FormatMessageWithArgumentsException, self).__init__(message, *args)
  26. class DeviceNotFoundError(FormatMessageWithArgumentsException):
  27. """Device isn't on USB."""
  28. class DeviceAuthError(FormatMessageWithArgumentsException):
  29. """Device authentication failed."""
  30. class LibusbWrappingError(CommonUsbError):
  31. """Wraps libusb1 errors while keeping its original usefulness.
  32. Attributes:
  33. usb_error: Instance of libusb1.USBError
  34. """
  35. def __init__(self, msg, usb_error):
  36. super(LibusbWrappingError, self).__init__(msg)
  37. self.usb_error = usb_error
  38. def __str__(self):
  39. return '%s: %s' % (
  40. super(LibusbWrappingError, self).__str__(), str(self.usb_error))
  41. class WriteFailedError(LibusbWrappingError):
  42. """Raised when the device doesn't accept our command."""
  43. class ReadFailedError(LibusbWrappingError):
  44. """Raised when the device doesn't respond to our commands."""
  45. class AdbCommandFailureException(Exception):
  46. """ADB Command returned a FAIL."""
  47. class AdbOperationException(Exception):
  48. """Failed to communicate over adb with device after multiple retries."""
  49. class TcpTimeoutException(FormatMessageWithArgumentsException):
  50. """TCP connection timed out in the time out given."""