exc.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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 QubesVMNotStartedError(QubesVMError):
  39. '''Domain is not started.
  40. This exception is thrown when machine is halted, but should be started
  41. (that is, either running or paused).
  42. '''
  43. def __init__(self, vm, msg=None):
  44. super(QubesVMNotStartedError, self).__init__(vm,
  45. msg or 'Domain is powered off: {!r}'.format(vm.name))
  46. class QubesVMNotRunningError(QubesVMNotStartedError):
  47. '''Domain is not running.
  48. This exception is thrown when machine should be running but is either
  49. halted or paused.
  50. '''
  51. def __init__(self, vm, msg=None):
  52. super(QubesVMNotRunningError, self).__init__(vm,
  53. msg or 'Domain not running (either powered off or paused): {!r}' \
  54. .format(vm.name))
  55. class QubesVMNotPausedError(QubesVMNotStartedError):
  56. '''Domain is not paused.
  57. This exception is thrown when machine should be paused, but is not.
  58. '''
  59. def __init__(self, vm, msg=None):
  60. super(QubesVMNotPausedError, self).__init__(vm,
  61. msg or 'Domain is not paused: {!r}'.format(vm.name))
  62. class QubesVMNotSuspendedError(QubesVMError):
  63. '''Domain is not suspended.
  64. This exception is thrown when machine should be suspended but is either
  65. halted or running.
  66. '''
  67. def __init__(self, vm, msg=None):
  68. super(QubesVMNotSuspendedError, self).__init__(vm,
  69. msg or 'Domain is not suspended: {!r}'.format(vm.name))
  70. class QubesVMNotHaltedError(QubesVMError):
  71. '''Domain is not halted.
  72. This exception is thrown when machine should be halted, but is not (either
  73. running or paused).
  74. '''
  75. def __init__(self, vm, msg=None):
  76. super(QubesVMNotHaltedError, self).__init__(vm,
  77. msg or 'Domain is not powered off: {!r}'.format(vm.name))
  78. class QubesNoTemplateError(QubesVMError):
  79. '''Cannot start domain, because there is no template'''
  80. def __init__(self, vm, msg=None):
  81. super(QubesNoTemplateError, self).__init__(
  82. msg or 'Template for the domain {!r} not found'.format(vm.name))
  83. class QubesValueError(QubesException, ValueError):
  84. '''Cannot set some value, because it is invalid, out of bounds, etc.'''
  85. pass
  86. class QubesPropertyValueError(QubesValueError):
  87. '''Cannot set value of qubes.property, because user-supplied value is wrong.
  88. '''
  89. def __init__(self, holder, prop, value, msg=None):
  90. super(QubesPropertyValueError, self).__init__(
  91. msg or 'Invalid value {!r} for property {!r} of {!r}'.format(
  92. value, prop.__name__, holder))
  93. self.holder = holder
  94. self.prop = prop
  95. self.value = value
  96. class QubesNotImplementedError(QubesException, NotImplementedError):
  97. '''Thrown at user when some feature is not implemented'''
  98. def __init__(self, msg=None):
  99. super(QubesNotImplementedError, self).__init__(
  100. msg or 'This feature is not available')
  101. class BackupCancelledError(QubesException):
  102. '''Thrown at user when backup was manually cancelled'''
  103. def __init__(self, msg=None):
  104. super(BackupCancelledError, self).__init__(
  105. msg or 'Backup cancelled')
  106. class QubesMemoryError(QubesException, MemoryError):
  107. '''Cannot start domain, because not enough memory is available'''
  108. def __init__(self, vm, msg=None):
  109. super(QubesMemoryError, self).__init__(
  110. msg or 'Not enough memory to start domain {!r}'.format(vm.name))
  111. self.vm = vm
  112. class QubesFeatureNotFoundError(QubesException, KeyError):
  113. '''Feature not set for a given domain'''
  114. def __init__(self, domain, feature):
  115. super(QubesFeatureNotFoundError, self).__init__(
  116. 'Feature not set for domain {}: {}'.format(domain, feature))
  117. self.feature = feature
  118. self.vm = domain