exc.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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 program is free software; you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation; either version 2 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # This program 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
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License along
  18. # with this program; if not, write to the Free Software Foundation, Inc.,
  19. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  20. #
  21. '''
  22. Qubes OS exception hierarchy
  23. '''
  24. class QubesException(Exception):
  25. '''Exception that can be shown to the user'''
  26. pass
  27. class QubesVMNotFoundError(QubesException, KeyError):
  28. '''Domain cannot be found in the system'''
  29. def __init__(self, vmname):
  30. super(QubesVMNotFoundError, self).__init__(
  31. 'No such domain: {!r}'.format(vmname))
  32. self.vmname = vmname
  33. class QubesVMError(QubesException):
  34. '''Some problem with domain state.'''
  35. def __init__(self, vm, msg):
  36. super(QubesVMError, self).__init__(msg)
  37. self.vm = vm
  38. class QubesVMInUseError(QubesVMError):
  39. '''VM is in use, cannot remove.'''
  40. def __init__(self, vm, msg=None):
  41. super(QubesVMInUseError, self).__init__(vm,
  42. msg or 'Domain is in use: {!r}'.format(vm.name))
  43. class QubesVMNotStartedError(QubesVMError):
  44. '''Domain is not started.
  45. This exception is thrown when machine is halted, but should be started
  46. (that is, either running or paused).
  47. '''
  48. def __init__(self, vm, msg=None):
  49. super(QubesVMNotStartedError, self).__init__(vm,
  50. msg or 'Domain is powered off: {!r}'.format(vm.name))
  51. class QubesVMNotRunningError(QubesVMNotStartedError):
  52. '''Domain is not running.
  53. This exception is thrown when machine should be running but is either
  54. halted or paused.
  55. '''
  56. def __init__(self, vm, msg=None):
  57. super(QubesVMNotRunningError, self).__init__(vm,
  58. msg or 'Domain not running (either powered off or paused): {!r}' \
  59. .format(vm.name))
  60. class QubesVMNotPausedError(QubesVMNotStartedError):
  61. '''Domain is not paused.
  62. This exception is thrown when machine should be paused, but is not.
  63. '''
  64. def __init__(self, vm, msg=None):
  65. super(QubesVMNotPausedError, self).__init__(vm,
  66. msg or 'Domain is not paused: {!r}'.format(vm.name))
  67. class QubesVMNotSuspendedError(QubesVMError):
  68. '''Domain is not suspended.
  69. This exception is thrown when machine should be suspended but is either
  70. halted or running.
  71. '''
  72. def __init__(self, vm, msg=None):
  73. super(QubesVMNotSuspendedError, self).__init__(vm,
  74. msg or 'Domain is not suspended: {!r}'.format(vm.name))
  75. class QubesVMNotHaltedError(QubesVMError):
  76. '''Domain is not halted.
  77. This exception is thrown when machine should be halted, but is not (either
  78. running or paused).
  79. '''
  80. def __init__(self, vm, msg=None):
  81. super(QubesVMNotHaltedError, self).__init__(vm,
  82. msg or 'Domain is not powered off: {!r}'.format(vm.name))
  83. class QubesNoTemplateError(QubesVMError):
  84. '''Cannot start domain, because there is no template'''
  85. def __init__(self, vm, msg=None):
  86. super(QubesNoTemplateError, self).__init__(vm,
  87. msg or 'Template for the domain {!r} not found'.format(vm.name))
  88. class QubesValueError(QubesException, ValueError):
  89. '''Cannot set some value, because it is invalid, out of bounds, etc.'''
  90. pass
  91. class QubesPropertyValueError(QubesValueError):
  92. '''Cannot set value of qubes.property, because user-supplied value is wrong.
  93. '''
  94. def __init__(self, holder, prop, value, msg=None):
  95. super(QubesPropertyValueError, self).__init__(
  96. msg or 'Invalid value {!r} for property {!r} of {!r}'.format(
  97. value, prop.__name__, holder))
  98. self.holder = holder
  99. self.prop = prop
  100. self.value = value
  101. class QubesNoSuchPropertyError(QubesException, AttributeError):
  102. '''Requested property does not exist
  103. '''
  104. def __init__(self, holder, prop_name, msg=None):
  105. super(QubesNoSuchPropertyError, self).__init__(
  106. msg or 'Invalid property {!r} of {!s}'.format(
  107. prop_name, holder))
  108. self.holder = holder
  109. self.prop = prop_name
  110. class QubesNotImplementedError(QubesException, NotImplementedError):
  111. '''Thrown at user when some feature is not implemented'''
  112. def __init__(self, msg=None):
  113. super(QubesNotImplementedError, self).__init__(
  114. msg or 'This feature is not available')
  115. class BackupCancelledError(QubesException):
  116. '''Thrown at user when backup was manually cancelled'''
  117. def __init__(self, msg=None):
  118. super(BackupCancelledError, self).__init__(
  119. msg or 'Backup cancelled')
  120. class QubesMemoryError(QubesException, MemoryError):
  121. '''Cannot start domain, because not enough memory is available'''
  122. def __init__(self, vm, msg=None):
  123. super(QubesMemoryError, self).__init__(
  124. msg or 'Not enough memory to start domain {!r}'.format(vm.name))
  125. self.vm = vm
  126. class QubesFeatureNotFoundError(QubesException, KeyError):
  127. '''Feature not set for a given domain'''
  128. def __init__(self, domain, feature):
  129. super(QubesFeatureNotFoundError, self).__init__(
  130. 'Feature not set for domain {}: {}'.format(domain, feature))
  131. self.feature = feature
  132. self.vm = domain
  133. class QubesTagNotFoundError(QubesException, KeyError):
  134. '''Tag not set for a given domain'''
  135. def __init__(self, domain, tag):
  136. super().__init__('Tag not set for domain {}: {}'.format(
  137. domain, tag))
  138. self.vm = domain
  139. self.tag = tag