exc.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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 QubesValueError(QubesException, ValueError):
  93. '''Cannot set some value, because it is invalid, out of bounds, etc.'''
  94. class QubesPropertyValueError(QubesValueError):
  95. '''Cannot set value of qubes.property, because user-supplied value is wrong.
  96. '''
  97. def __init__(self, holder, prop, value, msg=None):
  98. super(QubesPropertyValueError, self).__init__(
  99. msg or 'Invalid value {!r} for property {!r} of {!r}'.format(
  100. value, prop.__name__, holder))
  101. self.holder = holder
  102. self.prop = prop
  103. self.value = value
  104. class QubesNoSuchPropertyError(QubesException, AttributeError):
  105. '''Requested property does not exist
  106. '''
  107. def __init__(self, holder, prop_name, msg=None):
  108. super(QubesNoSuchPropertyError, self).__init__(
  109. msg or 'Invalid property {!r} of {!s}'.format(
  110. prop_name, holder))
  111. self.holder = holder
  112. self.prop = prop_name
  113. class QubesNotImplementedError(QubesException, NotImplementedError):
  114. '''Thrown at user when some feature is not implemented'''
  115. def __init__(self, msg=None):
  116. super(QubesNotImplementedError, self).__init__(
  117. msg or 'This feature is not available')
  118. class BackupCancelledError(QubesException):
  119. '''Thrown at user when backup was manually cancelled'''
  120. def __init__(self, msg=None):
  121. super(BackupCancelledError, self).__init__(
  122. msg or 'Backup cancelled')
  123. class QubesMemoryError(QubesVMError, MemoryError):
  124. '''Cannot start domain, because not enough memory is available'''
  125. def __init__(self, vm, msg=None):
  126. super(QubesMemoryError, self).__init__(vm,
  127. msg or 'Not enough memory to start domain {!r}'.format(vm.name))
  128. class QubesFeatureNotFoundError(QubesException, KeyError):
  129. '''Feature not set for a given domain'''
  130. def __init__(self, domain, feature):
  131. super(QubesFeatureNotFoundError, self).__init__(
  132. 'Feature not set for domain {}: {}'.format(domain, feature))
  133. self.feature = feature
  134. self.vm = domain
  135. class QubesTagNotFoundError(QubesException, KeyError):
  136. '''Tag not set for a given domain'''
  137. def __init__(self, domain, tag):
  138. super().__init__('Tag not set for domain {}: {}'.format(
  139. domain, tag))
  140. self.vm = domain
  141. self.tag = tag