exc.py 6.2 KB

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