xref: /openbmc/qemu/scripts/qapi/error.py (revision 6016b7b4)
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"""
13QAPI error classes
14
15Common error classes used throughout the package.  Additional errors may
16be defined in other modules.  At present, `QAPIParseError` is defined in
17parser.py.
18"""
19
20from typing import Optional
21
22from .source import QAPISourceInfo
23
24
25class QAPIError(Exception):
26    """Base class for all exceptions from the QAPI package."""
27
28
29class QAPISourceError(QAPIError):
30    """Error class for all exceptions identifying a source location."""
31    def __init__(self,
32                 info: Optional[QAPISourceInfo],
33                 msg: str,
34                 col: Optional[int] = None):
35        super().__init__()
36        self.info = info
37        self.msg = msg
38        self.col = col
39
40    def __str__(self) -> str:
41        assert self.info is not None
42        loc = str(self.info)
43        if self.col is not None:
44            assert self.info.line is not None
45            loc += ':%s' % self.col
46        return loc + ': ' + self.msg
47
48
49class QAPISemError(QAPISourceError):
50    """Error class for semantic QAPI errors."""
51