exc.py 6.0 KB

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