| 1 | # Copyright (c) 2017-2019 Red Hat Inc. |
| 2 | # |
| 3 | # Authors: |
| 4 | # Markus Armbruster <armbru@redhat.com> |
| 5 | # Marc-André Lureau <marcandre.lureau@redhat.com> |
| 6 | # |
| 7 | # This work is licensed under the terms of the GNU GPL, version 2. |
| 8 | # See the COPYING file in the top-level directory. |
| 9 | |
| 10 | """ |
| 11 | QAPI error classes |
| 12 | |
| 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 | |
| 18 | from typing import Optional |
| 19 | |
| 20 | from .source import QAPISourceInfo |
| 21 | |
| 22 | |
| 23 | class QAPIError(Exception): |
| 24 | """Base class for all exceptions from the QAPI package.""" |
| 25 | |
| 26 | |
| 27 | class QAPISourceError(QAPIError): |
| 28 | """Error class for all exceptions identifying a source location.""" |
| 29 | def __init__(self, |
| 30 | info: Optional[QAPISourceInfo], |
| 31 | msg: str, |
| 32 | col: Optional[int] = None): |
| 33 | super().__init__() |
| 34 | self.info = info |
| 35 | self.msg = msg |
| 36 | self.col = col |
| 37 | |
| 38 | def __str__(self) -> str: |
| 39 | assert self.info is not None |
| 40 | loc = str(self.info) |
| 41 | if self.col is not None: |
| 42 | assert self.info.line is not None |
| 43 | loc += ':%s' % self.col |
| 44 | return loc + ': ' + self.msg |
| 45 | |
| 46 | |
| 47 | class QAPISemError(QAPISourceError): |
| 48 | """Error class for semantic QAPI errors.""" |