xref: /openbmc/qemu/scripts/qapi/error.py (revision 1abdde1ad42d0ebccc5e8bc574ebe805cd650102)
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"""
11QAPI error classes
12
13Common error classes used throughout the package.  Additional errors may
14be defined in other modules.  At present, `QAPIParseError` is defined in
15parser.py.
16"""
17
18from typing import Optional
19
20from .source import QAPISourceInfo
21
22
23class QAPIError(Exception):
24    """Base class for all exceptions from the QAPI package."""
25
26
27class 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
47class QAPISemError(QAPISourceError):
48    """Error class for semantic QAPI errors."""
49