xref: /openbmc/qemu/scripts/qapi/error.py (revision 46f49468c690ff015a5b5346a279845f5e55369e)
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, col, msg):
22        Exception.__init__(self)
23        self.info = info
24        self.col = col
25        self.msg = msg
26
27    def __str__(self):
28        loc = str(self.info)
29        if self.col is not None:
30            assert self.info.line is not None
31            loc += ':%s' % self.col
32        return loc + ': ' + self.msg
33
34
35class QAPIParseError(QAPISourceError):
36    """Error class for all QAPI schema parsing errors."""
37    def __init__(self, parser, msg):
38        col = 1
39        for ch in parser.src[parser.line_pos:parser.pos]:
40            if ch == '\t':
41                col = (col + 7) % 8 + 1
42            else:
43                col += 1
44        super().__init__(parser.info, col, msg)
45
46
47class QAPISemError(QAPISourceError):
48    """Error class for semantic QAPI errors."""
49    def __init__(self, info, msg):
50        super().__init__(info, None, msg)
51