xref: /openbmc/qemu/scripts/qapi/error.py (revision fd990e86)
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    def __init__(self, info, col, msg):
17        Exception.__init__(self)
18        self.info = info
19        self.col = col
20        self.msg = msg
21
22    def __str__(self):
23        loc = str(self.info)
24        if self.col is not None:
25            assert self.info.line is not None
26            loc += ':%s' % self.col
27        return loc + ': ' + self.msg
28
29
30class QAPIParseError(QAPIError):
31    def __init__(self, parser, msg):
32        col = 1
33        for ch in parser.src[parser.line_pos:parser.pos]:
34            if ch == '\t':
35                col = (col + 7) % 8 + 1
36            else:
37                col += 1
38        super().__init__(parser.info, col, msg)
39
40
41class QAPISemError(QAPIError):
42    def __init__(self, info, msg):
43        super().__init__(info, None, msg)
44