exc.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. #
  2. # The Qubes OS Project, https://www.qubes-os.org/
  3. #
  4. # Copyright (C) 2015 Joanna Rutkowska <joanna@invisiblethingslab.com>
  5. # Copyright (C) 2015 Wojtek Porczyk <woju@invisiblethingslab.com>
  6. #
  7. # This library is free software; you can redistribute it and/or
  8. # modify it under the terms of the GNU Lesser General Public
  9. # License as published by the Free Software Foundation; either
  10. # version 2.1 of the License, or (at your option) any later version.
  11. #
  12. # This library is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. # Lesser General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU Lesser General Public
  18. # License along with this library; if not, see <https://www.gnu.org/licenses/>.
  19. #
  20. '''
  21. Qubes OS exception hierarchy
  22. '''
  23. class QubesException(Exception):
  24. '''Exception that can be shown to the user'''
  25. class QubesVMNotFoundError(QubesException, KeyError):
  26. '''Domain cannot be found in the system'''
  27. def __init__(self, vmname):
  28. super(QubesVMNotFoundError, self).__init__(
  29. 'No such domain: {!r}'.format(vmname))
  30. self.vmname = vmname
  31. class QubesVMError(QubesException):
  32. '''Some problem with domain state.'''
  33. def __init__(self, vm, msg):
  34. super(QubesVMError, self).__init__(msg)
  35. self.vm = vm
  36. class QubesVMInUseError(QubesVMError):
  37. '''VM is in use, cannot remove.'''
  38. def __init__(self, vm, msg=None):
  39. super(QubesVMInUseError, self).__init__(vm,
  40. msg or 'Domain is in use: {!r}'.format(vm.name))
  41. class QubesVMNotStartedError(QubesVMError):
  42. '''Domain is not started.
  43. This exception is thrown when machine is halted, but should be started
  44. (that is, either running or paused).
  45. '''
  46. def __init__(self, vm, msg=None):
  47. super(QubesVMNotStartedError, self).__init__(vm,
  48. msg or 'Domain is powered off: {!r}'.format(vm.name))
  49. class QubesVMNotRunningError(QubesVMNotStartedError):
  50. '''Domain is not running.
  51. This exception is thrown when machine should be running but is either
  52. halted or paused.
  53. '''
  54. def __init__(self, vm, msg=None):
  55. super(QubesVMNotRunningError, self).__init__(vm,
  56. msg or 'Domain not running (either powered off or paused): {!r}' \
  57. .format(vm.name))
  58. class QubesVMNotPausedError(QubesVMNotStartedError):
  59. '''Domain is not paused.
  60. This exception is thrown when machine should be paused, but is not.
  61. '''
  62. def __init__(self, vm, msg=None):
  63. super(QubesVMNotPausedError, self).__init__(vm,
  64. msg or 'Domain is not paused: {!r}'.format(vm.name))
  65. class QubesVMNotSuspendedError(QubesVMError):
  66. '''Domain is not suspended.
  67. This exception is thrown when machine should be suspended but is either
  68. halted or running.
  69. '''
  70. def __init__(self, vm, msg=None):
  71. super(QubesVMNotSuspendedError, self).__init__(vm,
  72. msg or 'Domain is not suspended: {!r}'.format(vm.name))
  73. class QubesVMNotHaltedError(QubesVMError):
  74. '''Domain is not halted.
  75. This exception is thrown when machine should be halted, but is not (either
  76. running or paused).
  77. '''
  78. def __init__(self, vm, msg=None):
  79. super(QubesVMNotHaltedError, self).__init__(vm,
  80. msg or 'Domain is not powered off: {!r}'.format(vm.name))
  81. class QubesVMShutdownTimeoutError(QubesVMError):
  82. '''Domain shutdown timed out.
  83. '''
  84. def __init__(self, vm, msg=None):
  85. super(QubesVMShutdownTimeoutError, self).__init__(vm,
  86. msg or 'Domain shutdown timed out: {!r}'.format(vm.name))
  87. class QubesNoTemplateError(QubesVMError):
  88. '''Cannot start domain, because there is no template'''
  89. def __init__(self, vm, msg=None):
  90. super(QubesNoTemplateError, self).__init__(vm,
  91. msg or 'Template for the domain {!r} not found'.format(vm.name))
  92. class QubesPoolInUseError(QubesException):
  93. '''VM is in use, cannot remove.'''
  94. def __init__(self, pool, msg=None):
  95. super(QubesPoolInUseError, self).__init__(
  96. msg or 'Storage pool is in use: {!r}'.format(pool.name))
  97. class QubesValueError(QubesException, ValueError):
  98. '''Cannot set some value, because it is invalid, out of bounds, etc.'''
  99. class QubesPropertyValueError(QubesValueError):
  100. '''Cannot set value of qubes.property, because user-supplied value is wrong.
  101. '''
  102. def __init__(self, holder, prop, value, msg=None):
  103. super(QubesPropertyValueError, self).__init__(
  104. msg or 'Invalid value {!r} for property {!r} of {!r}'.format(
  105. value, prop.__name__, holder))
  106. self.holder = holder
  107. self.prop = prop
  108. self.value = value
  109. class QubesNoSuchPropertyError(QubesException, AttributeError):
  110. '''Requested property does not exist
  111. '''
  112. def __init__(self, holder, prop_name, msg=None):
  113. super(QubesNoSuchPropertyError, self).__init__(
  114. msg or 'Invalid property {!r} of {!s}'.format(
  115. prop_name, holder))
  116. self.holder = holder
  117. self.prop = prop_name
  118. class QubesNotImplementedError(QubesException, NotImplementedError):
  119. '''Thrown at user when some feature is not implemented'''
  120. def __init__(self, msg=None):
  121. super(QubesNotImplementedError, self).__init__(
  122. msg or 'This feature is not available')
  123. class BackupCancelledError(QubesException):
  124. '''Thrown at user when backup was manually cancelled'''
  125. def __init__(self, msg=None):
  126. super(BackupCancelledError, self).__init__(
  127. msg or 'Backup cancelled')
  128. class QubesMemoryError(QubesVMError, MemoryError):
  129. '''Cannot start domain, because not enough memory is available'''
  130. def __init__(self, vm, msg=None):
  131. super(QubesMemoryError, self).__init__(vm,
  132. msg or 'Not enough memory to start domain {!r}'.format(vm.name))
  133. class QubesFeatureNotFoundError(QubesException, KeyError):
  134. '''Feature not set for a given domain'''
  135. def __init__(self, domain, feature):
  136. super(QubesFeatureNotFoundError, self).__init__(
  137. 'Feature not set for domain {}: {}'.format(domain, feature))
  138. self.feature = feature
  139. self.vm = domain
  140. class QubesTagNotFoundError(QubesException, KeyError):
  141. '''Tag not set for a given domain'''
  142. def __init__(self, domain, tag):
  143. super().__init__('Tag not set for domain {}: {}'.format(
  144. domain, tag))
  145. self.vm = domain
  146. self.tag = tag