Coverage for pydelica/exception.py: 76%
66 statements
« prev ^ index » next coverage.py v7.8.0, created at 2025-05-19 07:38 +0000
« prev ^ index » next coverage.py v7.8.0, created at 2025-05-19 07:38 +0000
1import re
4class BinaryNotFoundError(Exception):
5 def __init__(self, msg):
6 Exception.__init__(self, msg)
9class OMParsingError(Exception):
10 def __init__(self, msg):
11 Exception.__init__(self, msg)
14class OMExecutionError(Exception):
15 def __init__(self, msg):
16 Exception.__init__(self, msg)
19class OMBuildError(Exception):
20 def __init__(self, msg):
21 Exception.__init__(self, msg)
24class OMAssertionError(Exception):
25 def __init__(self, msg):
26 Exception.__init__(self, msg)
29class NotImplementedError(Exception):
30 def __init__(self, msg):
31 Exception.__init__(self, msg)
34class UnknownParameterError(Exception):
35 def __init__(self, param_name: str):
36 msg = f"Parameter '{param_name}' is not a recognised parameter name"
37 Exception.__init__(self, msg)
40class UnknownModelError(Exception):
41 def __init__(self, model_name: str):
42 msg = f"Model '{model_name}' is not a recognised model name."
43 Exception.__init__(self, msg)
46class ResultRetrievalError(Exception):
47 def __init__(self):
48 msg = "Failed to retrieve simulation results, could not read output files."
49 Exception.__init__(self, msg)
52class UnknownOptionError(Exception):
53 def __init__(self, opt_name: str):
54 msg = f"Option '{opt_name}' is not a recognised simulation option"
55 Exception.__init__(self, msg)
58class ModelicaFileGenerationError(Exception):
59 def __init__(self, msg):
60 Exception.__init__(self, msg)
63class UnknownLibraryError(Exception):
64 def __init__(self, msg):
65 Exception.__init__(self, msg)
68def parse_error_string_compiler(out_string: str, error_string: str):
69 if "Failed to parse file" in out_string:
70 print(out_string)
71 _error = [i for i in out_string.split("\n") if i and i[0] == "["]
72 raise OMParsingError(", ".join(_error))
73 elif "Execution failed!" in error_string:
74 raise OMExecutionError(f"Failed to execute compiled code:\n{out_string}")
76 # Check if "failed" is present within output, ignore print out of code
77 # assuming all lines containing the term end with ';'
79 _lines: list[str] = [
80 i for i in out_string.split("\n")
81 if "failed" in i.lower()
82 and not i.strip().endswith(";") # Do not include lines from code
83 ]
85 if "failed" in out_string and _lines:
86 raise OMBuildError(
87 ", ".join(_lines)
88 )
91def parse_error_string_simulate(out_string: str, terminate_on_assert: str = "error"):
92 print(out_string)
93 if "division by zero" in out_string:
94 _line = [i for i in out_string.split("\n") if "division by zero" in i]
95 raise ZeroDivisionError(_line[0].split("|")[-1].strip())
96 elif "simulation terminated by an assertion" in out_string:
97 raise OMAssertionError(f"Simulation run failed:\n{out_string}")
99 _find_assert = re.compile(r"assert\s*\|\s*(\w+)\s*\|", re.IGNORECASE)
100 _asserts = _find_assert.findall(out_string)
102 _assertion_ranking = ("debug", "info", "warning", "error", "never")
104 if not _asserts:
105 return
107 _assertion_rank_pass = [
108 _assertion_ranking.index(i) >= _assertion_ranking.index(terminate_on_assert)
109 for i in _asserts
110 ]
112 if any(_assertion_rank_pass):
113 raise OMAssertionError(out_string)