1e6c42b96SMarkus Armbruster# -*- coding: utf-8 -*- 2e6c42b96SMarkus Armbruster# 3e6c42b96SMarkus Armbruster# Copyright (c) 2017-2019 Red Hat Inc. 4e6c42b96SMarkus Armbruster# 5e6c42b96SMarkus Armbruster# Authors: 6e6c42b96SMarkus Armbruster# Markus Armbruster <armbru@redhat.com> 7e6c42b96SMarkus Armbruster# Marc-André Lureau <marcandre.lureau@redhat.com> 8e6c42b96SMarkus Armbruster# 9e6c42b96SMarkus Armbruster# This work is licensed under the terms of the GNU GPL, version 2. 10e6c42b96SMarkus Armbruster# See the COPYING file in the top-level directory. 11e6c42b96SMarkus Armbruster 12ac6a7d88SJohn Snow""" 13ac6a7d88SJohn SnowQAPI error classes 14ac6a7d88SJohn Snow 15ac6a7d88SJohn SnowCommon error classes used throughout the package. Additional errors may 16ac6a7d88SJohn Snowbe defined in other modules. At present, `QAPIParseError` is defined in 17ac6a7d88SJohn Snowparser.py. 18ac6a7d88SJohn Snow""" 19ac6a7d88SJohn Snow 20*30d0a016SJohn Snowfrom typing import Optional 21*30d0a016SJohn Snow 22*30d0a016SJohn Snowfrom .source import QAPISourceInfo 23*30d0a016SJohn Snow 24e6c42b96SMarkus Armbruster 25e6c42b96SMarkus Armbrusterclass QAPIError(Exception): 2646f49468SJohn Snow """Base class for all exceptions from the QAPI package.""" 2746f49468SJohn Snow 2846f49468SJohn Snow 2946f49468SJohn Snowclass QAPISourceError(QAPIError): 3046f49468SJohn Snow """Error class for all exceptions identifying a source location.""" 31*30d0a016SJohn Snow def __init__(self, 32*30d0a016SJohn Snow info: Optional[QAPISourceInfo], 33*30d0a016SJohn Snow msg: str, 34*30d0a016SJohn Snow col: Optional[int] = None): 35b54e07ccSJohn Snow super().__init__() 36e6c42b96SMarkus Armbruster self.info = info 37e6c42b96SMarkus Armbruster self.msg = msg 3886cc2ff6SJohn Snow self.col = col 39e6c42b96SMarkus Armbruster 40*30d0a016SJohn Snow def __str__(self) -> str: 41ac897611SJohn Snow assert self.info is not None 42e6c42b96SMarkus Armbruster loc = str(self.info) 43e6c42b96SMarkus Armbruster if self.col is not None: 44e6c42b96SMarkus Armbruster assert self.info.line is not None 45e6c42b96SMarkus Armbruster loc += ':%s' % self.col 46e6c42b96SMarkus Armbruster return loc + ': ' + self.msg 47e6c42b96SMarkus Armbruster 48e6c42b96SMarkus Armbruster 4946f49468SJohn Snowclass QAPISemError(QAPISourceError): 5046f49468SJohn Snow """Error class for semantic QAPI errors.""" 51