xref: /openbmc/qemu/scripts/qapi/error.py (revision ac6a7d8884762d27cc2dde5a5c6e793cc18fc4d9)
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
20
21class QAPIError(Exception):
22    """Base class for all exceptions from the QAPI package."""
23
24
25class QAPISourceError(QAPIError):
26    """Error class for all exceptions identifying a source location."""
27    def __init__(self, info, msg, col=None):
28        super().__init__()
29        self.info = info
30        self.msg = msg
31        self.col = col
32
33    def __str__(self):
34        assert self.info is not None
35        loc = str(self.info)
36        if self.col is not None:
37            assert self.info.line is not None
38            loc += ':%s' % self.col
39        return loc + ': ' + self.msg
40
41
42class QAPISemError(QAPISourceError):
43    """Error class for semantic QAPI errors."""
44