Coverage for lobster/common/errors.py: 89%
40 statements
« prev ^ index » next coverage.py v7.10.5, created at 2025-08-27 13:02 +0000
« prev ^ index » next coverage.py v7.10.5, created at 2025-08-27 13:02 +0000
1#!/usr/bin/env python3
2#
3# LOBSTER - Lightweight Open BMW Software Traceability Evidence Report
4# Copyright (C) 2022-2023, 2025 Bayerische Motoren Werke Aktiengesellschaft (BMW AG)
5#
6# This program is free software: you can redistribute it and/or modify
7# it under the terms of the GNU Affero General Public License as
8# published by the Free Software Foundation, either version 3 of the
9# License, or (at your option) any later version.
10#
11# This program is distributed in the hope that it will be useful, but
12# WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14# Affero General Public License for more details.
15#
16# You should have received a copy of the GNU Affero General Public
17# License along with this program. If not, see
18# <https://www.gnu.org/licenses/>.
20from lobster.common.exceptions import LOBSTER_Exception
21from lobster.common.location import Location
24class PathError(Exception):
25 pass
28class LOBSTER_Error(LOBSTER_Exception):
29 def __init__(self, location, message):
30 super().__init__(message)
31 assert isinstance(location, Location)
32 assert isinstance(message, str)
33 self.location = location
34 self.message = message
36 def dump(self):
37 print(self.location.to_string() + ": " + self.message)
40class Message_Handler:
41 def __init__(self):
42 self.warnings = 0
43 self.errors = 0
45 def emit(self, location, severity, message):
46 assert isinstance(location, Location)
47 assert severity in ("warning", "lex error", "error")
48 assert isinstance(message, str)
50 if severity == "warning":
51 self.warnings += 1
52 else:
53 self.errors += 1
55 print(f"{location.to_string()}: lobster {severity}: {message}")
57 def lex_error(self, location, message):
58 assert isinstance(location, Location)
59 assert isinstance(message, str)
61 self.emit(location, "lex error", message)
62 raise LOBSTER_Error(location, message)
64 def error(self, location, message, fatal=True):
65 assert isinstance(location, Location)
66 assert isinstance(message, str)
68 self.emit(location, "error", message)
69 if fatal:
70 raise LOBSTER_Error(location, message)
72 def warning(self, location, message):
73 assert isinstance(location, Location)
74 assert isinstance(message, str)
76 self.emit(location, "warning", message)