exc.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. # -*- encoding: utf8 -*-
  2. #
  3. # The Qubes OS Project, http://www.qubes-os.org
  4. #
  5. # Copyright (C) 2017 Marek Marczykowski-Górecki
  6. # <marmarek@invisiblethingslab.com>
  7. #
  8. # This program is free software; you can redistribute it and/or modify
  9. # it under the terms of the GNU Lesser General Public License as published by
  10. # the Free Software Foundation; either version 2.1 of the License, or
  11. # (at your option) any later version.
  12. #
  13. # This program is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU Lesser General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU Lesser General Public License along
  19. # with this program; if not, see <http://www.gnu.org/licenses/>.
  20. '''Exception hierarchy.'''
  21. class QubesException(Exception):
  22. '''Base exception for all Qubes-related errors.'''
  23. def __init__(self, message_format, *args, **kwargs):
  24. # TODO: handle translations
  25. super().__init__(
  26. message_format % tuple(int(d) if d.isdigit() else d for d in args),
  27. **kwargs)
  28. class QubesVMNotFoundError(QubesException, KeyError):
  29. '''Domain cannot be found in the system'''
  30. def __str__(self):
  31. # KeyError overrides __str__ method
  32. return QubesException.__str__(self)
  33. class QubesVMError(QubesException):
  34. '''Some problem with domain state.'''
  35. class QubesVMNotStartedError(QubesVMError):
  36. '''Domain is not started.
  37. This exception is thrown when machine is halted, but should be started
  38. (that is, either running or paused).
  39. '''
  40. class QubesVMNotRunningError(QubesVMNotStartedError):
  41. '''Domain is not running.
  42. This exception is thrown when machine should be running but is either
  43. halted or paused.
  44. '''
  45. class QubesVMNotPausedError(QubesVMNotStartedError):
  46. '''Domain is not paused.
  47. This exception is thrown when machine should be paused, but is not.
  48. '''
  49. class QubesVMNotSuspendedError(QubesVMError):
  50. '''Domain is not suspended.
  51. This exception is thrown when machine should be suspended but is either
  52. halted or running.
  53. '''
  54. class QubesVMNotHaltedError(QubesVMError):
  55. '''Domain is not halted.
  56. This exception is thrown when machine should be halted, but is not (either
  57. running or paused).
  58. '''
  59. class QubesVMShutdownTimeout(QubesVMError):
  60. ''' Domain shutdown haven't completed in expected timeframe'''
  61. class QubesNoTemplateError(QubesVMError):
  62. '''Cannot start domain, because there is no template'''
  63. class QubesVMInUseError(QubesVMError):
  64. '''VM is in use, cannot remove.'''
  65. class QubesValueError(QubesException, ValueError):
  66. '''Cannot set some value, because it is invalid, out of bounds, etc.'''
  67. class QubesPropertyValueError(QubesValueError):
  68. '''Cannot set value of qubes.property, because user-supplied value is wrong.
  69. '''
  70. class QubesNoSuchPropertyError(QubesException, AttributeError):
  71. '''Requested property does not exist
  72. '''
  73. class QubesNotImplementedError(QubesException, NotImplementedError):
  74. '''Thrown at user when some feature is not implemented'''
  75. class BackupCancelledError(QubesException):
  76. '''Thrown at user when backup was manually cancelled'''
  77. class BackupAlreadyRunningError(QubesException):
  78. '''Thrown at user when they try to run the same backup twice at
  79. the same time'''
  80. class QubesMemoryError(QubesException, MemoryError):
  81. '''Cannot start domain, because not enough memory is available'''
  82. class QubesFeatureNotFoundError(QubesException, KeyError):
  83. '''Feature not set for a given domain'''
  84. def __str__(self):
  85. # KeyError overrides __str__ method
  86. return QubesException.__str__(self)
  87. class QubesTagNotFoundError(QubesException, KeyError):
  88. '''Tag not set for a given domain'''
  89. def __str__(self):
  90. # KeyError overrides __str__ method
  91. return QubesException.__str__(self)
  92. class QubesLabelNotFoundError(QubesException, KeyError):
  93. """Label does not exists"""
  94. def __str__(self):
  95. # KeyError overrides __str__ method
  96. return QubesException.__str__(self)
  97. class StoragePoolException(QubesException):
  98. ''' A general storage exception '''
  99. class QubesDaemonCommunicationError(QubesException, IOError):
  100. '''Error while communicating with qubesd, may mean insufficient
  101. permissions as well'''
  102. class DeviceAlreadyAttached(QubesException, KeyError):
  103. '''Trying to attach already attached device'''
  104. def __str__(self):
  105. # KeyError overrides __str__ method
  106. return QubesException.__str__(self)
  107. class BackupRestoreError(QubesException):
  108. '''Restoring a backup failed'''
  109. def __init__(self, msg, backup_log=None):
  110. super().__init__(msg)
  111. self.backup_log = backup_log
  112. # pylint: disable=too-many-ancestors
  113. class QubesDaemonAccessError(QubesDaemonCommunicationError):
  114. '''Got empty response from qubesd. This can be lack of permission,
  115. or some server-side issue.'''
  116. class QubesPropertyAccessError(QubesDaemonAccessError, AttributeError):
  117. '''Failed to read/write property value, cause is unknown (insufficient
  118. permissions, no such property, invalid value, other)'''
  119. def __init__(self, prop):
  120. super().__init__('Failed to access \'%s\' property' % prop)
  121. # legacy name
  122. QubesDaemonNoResponseError = QubesDaemonAccessError