error.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. # -*- coding: utf-8 -*-
  2. #
  3. # Copyright (c) 2017-2019 Red Hat Inc.
  4. #
  5. # Authors:
  6. # Markus Armbruster <armbru@redhat.com>
  7. # Marc-André Lureau <marcandre.lureau@redhat.com>
  8. #
  9. # This work is licensed under the terms of the GNU GPL, version 2.
  10. # See the COPYING file in the top-level directory.
  11. """
  12. QAPI error classes
  13. Common error classes used throughout the package. Additional errors may
  14. be defined in other modules. At present, `QAPIParseError` is defined in
  15. parser.py.
  16. """
  17. from typing import Optional
  18. from .source import QAPISourceInfo
  19. class QAPIError(Exception):
  20. """Base class for all exceptions from the QAPI package."""
  21. class QAPISourceError(QAPIError):
  22. """Error class for all exceptions identifying a source location."""
  23. def __init__(self,
  24. info: Optional[QAPISourceInfo],
  25. msg: str,
  26. col: Optional[int] = None):
  27. super().__init__()
  28. self.info = info
  29. self.msg = msg
  30. self.col = col
  31. def __str__(self) -> str:
  32. assert self.info is not None
  33. loc = str(self.info)
  34. if self.col is not None:
  35. assert self.info.line is not None
  36. loc += ':%s' % self.col
  37. return loc + ': ' + self.msg
  38. class QAPISemError(QAPISourceError):
  39. """Error class for semantic QAPI errors."""