exc.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. #!/usr/bin/python2 -O
  2. # vim: fileencoding=utf-8
  3. #
  4. # The Qubes OS Project, https://www.qubes-os.org/
  5. #
  6. # Copyright (C) 2015 Joanna Rutkowska <joanna@invisiblethingslab.com>
  7. # Copyright (C) 2015 Wojtek Porczyk <woju@invisiblethingslab.com>
  8. #
  9. # This program is free software; you can redistribute it and/or modify
  10. # it under the terms of the GNU General Public License as published by
  11. # the Free Software Foundation; either version 2 of the License, or
  12. # (at your option) any later version.
  13. #
  14. # This program is distributed in the hope that it will be useful,
  15. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. # GNU General Public License for more details.
  18. #
  19. # You should have received a copy of the GNU General Public License along
  20. # with this program; if not, write to the Free Software Foundation, Inc.,
  21. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  22. #
  23. '''
  24. Qubes OS exception hierarchy
  25. '''
  26. class QubesException(Exception):
  27. '''Exception that can be shown to the user'''
  28. pass
  29. class QubesVMNotFoundError(QubesException, KeyError):
  30. '''Domain cannot be found in the system'''
  31. def __init__(self, vmname):
  32. super(QubesVMNotFoundError, self).__init__(
  33. 'No such domain: {!r}'.format(vmname))
  34. self.vmname = vmname
  35. class QubesVMError(QubesException):
  36. '''Some problem with domain state.'''
  37. def __init__(self, vm, msg):
  38. super(QubesVMError, self).__init__(msg)
  39. self.vm = vm
  40. class QubesVMNotStartedError(QubesVMError):
  41. '''Domain is not started.
  42. This exception is thrown when machine is halted, but should be started
  43. (that is, either running or paused).
  44. '''
  45. def __init__(self, vm, msg=None):
  46. super(QubesVMNotStartedError, self).__init__(vm,
  47. msg or 'Domain is powered off: {!r}'.format(vm.name))
  48. class QubesVMNotRunningError(QubesVMNotStartedError):
  49. '''Domain is not running.
  50. This exception is thrown when machine should be running but is either
  51. halted or paused.
  52. '''
  53. def __init__(self, vm, msg=None):
  54. super(QubesVMNotRunningError, self).__init__(vm,
  55. msg or 'Domain not running (either powered off or paused): {!r}' \
  56. .format(vm.name))
  57. class QubesVMNotPausedError(QubesVMNotStartedError):
  58. '''Domain is not paused.
  59. This exception is thrown when machine should be paused, but is not.
  60. '''
  61. def __init__(self, vm, msg=None):
  62. super(QubesVMNotPausedError, self).__init__(vm,
  63. msg or 'Domain is not paused: {!r}'.format(vm.name))
  64. class QubesVMNotSuspendedError(QubesVMError):
  65. '''Domain is not suspended.
  66. This exception is thrown when machine should be suspended but is either
  67. halted or running.
  68. '''
  69. def __init__(self, vm, msg=None):
  70. super(QubesVMNotSuspendedError, self).__init__(vm,
  71. msg or 'Domain is not suspended: {!r}'.format(vm.name))
  72. class QubesVMNotHaltedError(QubesVMError):
  73. '''Domain is not halted.
  74. This exception is thrown when machine should be halted, but is not (either
  75. running or paused).
  76. '''
  77. def __init__(self, vm, msg=None):
  78. super(QubesVMNotHaltedError, self).__init__(vm,
  79. msg or 'Domain is not powered off: {!r}'.format(vm.name))
  80. class QubesNoTemplateError(QubesVMError):
  81. '''Cannot start domain, because there is no template'''
  82. def __init__(self, vm, msg=None):
  83. super(QubesNoTemplateError, self).__init__(
  84. msg or 'Template for the domain {!r} not found'.format(vm.name))
  85. class QubesValueError(QubesException, ValueError):
  86. '''Cannot set some value, because it is invalid, out of bounds, etc.'''
  87. pass
  88. class QubesPropertyValueError(QubesValueError):
  89. '''Cannot set value of qubes.property, because user-supplied value is wrong.
  90. '''
  91. def __init__(self, holder, prop, value, msg=None):
  92. super(QubesPropertyValueError, self).__init__(
  93. msg or 'Invalid value {!r} for property {!r} of {!r}'.format(
  94. value, prop.__name__, holder))
  95. self.holder = holder
  96. self.prop = prop
  97. self.value = value
  98. class QubesNotImplementedError(QubesException, NotImplementedError):
  99. '''Thrown at user when some feature is not implemented'''
  100. def __init__(self, msg=None):
  101. super(QubesNotImplementedError, self).__init__(
  102. msg or 'This feature is not available')
  103. class BackupCancelledError(QubesException):
  104. '''Thrown at user when backup was manually cancelled'''
  105. def __init__(self, msg=None):
  106. super(BackupCancelledError, self).__init__(
  107. msg or 'Backup cancelled')
  108. class QubesMemoryError(QubesException, MemoryError):
  109. '''Cannot start domain, because not enough memory is available'''
  110. def __init__(self, vm, msg=None):
  111. super(QubesMemoryError, self).__init__(
  112. msg or 'Not enough memory to start domain {!r}'.format(vm.name))
  113. self.vm = vm