3fbc14d377
* Rename _typing to beancount_types to better reflect what it is. * LessComparable isn't a Beancount type, so move that to plugin.core with its dependent helper classes. * Errors are a core Beancount concept, so move that module to the top level and have it include appropriate type definitions.
47 lines
1.6 KiB
Python
47 lines
1.6 KiB
Python
"""Error classes for plugins to report problems in the books"""
|
|
# Copyright © 2020 Brett Smith
|
|
#
|
|
# This program is free software: you can redistribute it and/or modify
|
|
# it under the terms of the GNU Affero General Public License as published by
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
# (at your option) any later version.
|
|
#
|
|
# This program is distributed in the hope that it will be useful,
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
# GNU Affero General Public License for more details.
|
|
#
|
|
# You should have received a copy of the GNU Affero General Public License
|
|
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
from typing import (
|
|
Iterable,
|
|
)
|
|
|
|
class Error(Exception):
|
|
def __init__(self, message, entry, source=None):
|
|
self.message = message
|
|
self.entry = entry
|
|
self.source = entry.meta if source is None else source
|
|
|
|
def __repr__(self):
|
|
return "{clsname}<{source[filename]}:{source[lineno]}: {message}>".format(
|
|
clsname=type(self).__name__,
|
|
message=self.message,
|
|
source=self.source,
|
|
)
|
|
|
|
|
|
Iter = Iterable[Error]
|
|
|
|
class InvalidMetadataError(Error):
|
|
def __init__(self, txn, post, key, value=None, source=None):
|
|
if value is None:
|
|
msg_fmt = "{post.account} missing {key}"
|
|
else:
|
|
msg_fmt = "{post.account} has invalid {key}: {value}"
|
|
super().__init__(
|
|
msg_fmt.format(post=post, key=key, value=value),
|
|
txn,
|
|
source,
|
|
)
|