exc.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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().__init__(
  29. 'No such domain: {!r}'.format(vmname))
  30. self.vmname = vmname
  31. def __str__(self):
  32. # KeyError overrides __str__ method
  33. return QubesException.__str__(self)
  34. class QubesVMError(QubesException):
  35. '''Some problem with domain state.'''
  36. def __init__(self, vm, msg):
  37. super().__init__(msg)
  38. self.vm = vm
  39. class QubesVMInUseError(QubesVMError):
  40. '''VM is in use, cannot remove.'''
  41. def __init__(self, vm, msg=None):
  42. super().__init__(vm,
  43. msg or 'Domain is in use: {!r}'.format(vm.name))
  44. class QubesVMNotStartedError(QubesVMError):
  45. '''Domain is not started.
  46. This exception is thrown when machine is halted, but should be started
  47. (that is, either running or paused).
  48. '''
  49. def __init__(self, vm, msg=None):
  50. super().__init__(vm,
  51. msg or 'Domain is powered off: {!r}'.format(vm.name))
  52. class QubesVMNotRunningError(QubesVMNotStartedError):
  53. '''Domain is not running.
  54. This exception is thrown when machine should be running but is either
  55. halted or paused.
  56. '''
  57. def __init__(self, vm, msg=None):
  58. super().__init__(vm,
  59. msg or 'Domain not running (either powered off or paused): {!r}' \
  60. .format(vm.name))
  61. class QubesVMNotPausedError(QubesVMNotStartedError):
  62. '''Domain is not paused.
  63. This exception is thrown when machine should be paused, but is not.
  64. '''
  65. def __init__(self, vm, msg=None):
  66. super().__init__(vm,
  67. msg or 'Domain is not paused: {!r}'.format(vm.name))
  68. class QubesVMNotSuspendedError(QubesVMError):
  69. '''Domain is not suspended.
  70. This exception is thrown when machine should be suspended but is either
  71. halted or running.
  72. '''
  73. def __init__(self, vm, msg=None):
  74. super().__init__(vm,
  75. msg or 'Domain is not suspended: {!r}'.format(vm.name))
  76. class QubesVMNotHaltedError(QubesVMError):
  77. '''Domain is not halted.
  78. This exception is thrown when machine should be halted, but is not (either
  79. running or paused).
  80. '''
  81. def __init__(self, vm, msg=None):
  82. super().__init__(vm,
  83. msg or 'Domain is not powered off: {!r}'.format(vm.name))
  84. class QubesVMShutdownTimeoutError(QubesVMError):
  85. '''Domain shutdown timed out.
  86. '''
  87. def __init__(self, vm, msg=None):
  88. super().__init__(vm,
  89. msg or 'Domain shutdown timed out: {!r}'.format(vm.name))
  90. class QubesNoTemplateError(QubesVMError):
  91. '''Cannot start domain, because there is no template'''
  92. def __init__(self, vm, msg=None):
  93. super().__init__(vm,
  94. msg or 'Template for the domain {!r} not found'.format(vm.name))
  95. class QubesPoolInUseError(QubesException):
  96. '''VM is in use, cannot remove.'''
  97. def __init__(self, pool, msg=None):
  98. super().__init__(
  99. msg or 'Storage pool is in use: {!r}'.format(pool.name))
  100. class QubesValueError(QubesException, ValueError):
  101. '''Cannot set some value, because it is invalid, out of bounds, etc.'''
  102. class QubesPropertyValueError(QubesValueError):
  103. '''Cannot set value of qubes.property, because user-supplied value is wrong.
  104. '''
  105. def __init__(self, holder, prop, value, msg=None):
  106. super().__init__(
  107. msg or 'Invalid value {!r} for property {!r} of {!r}'.format(
  108. value, prop.__name__, holder))
  109. self.holder = holder
  110. self.prop = prop
  111. self.value = value
  112. class QubesNoSuchPropertyError(QubesException, AttributeError):
  113. '''Requested property does not exist
  114. '''
  115. def __init__(self, holder, prop_name, msg=None):
  116. super().__init__(
  117. msg or 'Invalid property {!r} of {!s}'.format(
  118. prop_name, holder))
  119. self.holder = holder
  120. self.prop = prop_name
  121. class QubesNotImplementedError(QubesException, NotImplementedError):
  122. '''Thrown at user when some feature is not implemented'''
  123. def __init__(self, msg=None):
  124. super().__init__(
  125. msg or 'This feature is not available')
  126. class BackupCancelledError(QubesException):
  127. '''Thrown at user when backup was manually cancelled'''
  128. def __init__(self, msg=None):
  129. super().__init__(
  130. msg or 'Backup cancelled')
  131. class BackupAlreadyRunningError(QubesException):
  132. '''Thrown at user when they try to run the same backup twice at
  133. the same time'''
  134. def __init__(self, msg=None):
  135. super().__init__(
  136. msg or 'Backup already running')
  137. class QubesMemoryError(QubesVMError, MemoryError):
  138. '''Cannot start domain, because not enough memory is available'''
  139. def __init__(self, vm, msg=None):
  140. super().__init__(vm,
  141. msg or 'Not enough memory to start domain {!r}'.format(vm.name))
  142. class QubesFeatureNotFoundError(QubesException, KeyError):
  143. '''Feature not set for a given domain'''
  144. def __init__(self, domain, feature):
  145. super().__init__(
  146. 'Feature not set for domain {}: {}'.format(domain, feature))
  147. self.feature = feature
  148. self.vm = domain
  149. def __str__(self):
  150. # KeyError overrides __str__ method
  151. return QubesException.__str__(self)
  152. class QubesTagNotFoundError(QubesException, KeyError):
  153. '''Tag not set for a given domain'''
  154. def __init__(self, domain, tag):
  155. super().__init__('Tag not set for domain {}: {}'.format(
  156. domain, tag))
  157. self.vm = domain
  158. self.tag = tag
  159. def __str__(self):
  160. # KeyError overrides __str__ method
  161. return QubesException.__str__(self)
  162. class QubesLabelNotFoundError(QubesException, KeyError):
  163. """Label does not exists"""
  164. def __init__(self, label):
  165. super().__init__('Label does not exist: {}'.format(label))
  166. self.label = label
  167. def __str__(self):
  168. # KeyError overrides __str__ method
  169. return QubesException.__str__(self)