xref: /openbmc/qemu/scripts/qapi/error.py (revision ac89761179ed6e3165a63ad68759f77f33bace30)
1# -*- coding: utf-8 -*-
2#
3# QAPI error classes
4#
5# Copyright (c) 2017-2019 Red Hat Inc.
6#
7# Authors:
8#  Markus Armbruster <armbru@redhat.com>
9#  Marc-André Lureau <marcandre.lureau@redhat.com>
10#
11# This work is licensed under the terms of the GNU GPL, version 2.
12# See the COPYING file in the top-level directory.
13
14
15class QAPIError(Exception):
16    """Base class for all exceptions from the QAPI package."""
17
18
19class QAPISourceError(QAPIError):
20    """Error class for all exceptions identifying a source location."""
21    def __init__(self, info, msg, col=None):
22        super().__init__()
23        self.info = info
24        self.msg = msg
25        self.col = col
26
27    def __str__(self):
28        assert self.info is not None
29        loc = str(self.info)
30        if self.col is not None:
31            assert self.info.line is not None
32            loc += ':%s' % self.col
33        return loc + ': ' + self.msg
34
35
36class QAPIParseError(QAPISourceError):
37    """Error class for all QAPI schema parsing errors."""
38    def __init__(self, parser, msg):
39        col = 1
40        for ch in parser.src[parser.line_pos:parser.pos]:
41            if ch == '\t':
42                col = (col + 7) % 8 + 1
43            else:
44                col += 1
45        super().__init__(parser.info, msg, col)
46
47
48class QAPISemError(QAPISourceError):
49    """Error class for semantic QAPI errors."""
50