Skip to content

Commit 4c03f76

Browse files
committed
user defined errors are dataclasses
1 parent f36d3b4 commit 4c03f76

File tree

1 file changed

+42
-106
lines changed

1 file changed

+42
-106
lines changed

src/stagpy/error.py

Lines changed: 42 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from __future__ import annotations
44

55
import typing
6+
from dataclasses import dataclass
67

78
if typing.TYPE_CHECKING:
89
from pathlib import Path
@@ -21,166 +22,101 @@ class StagpyError(Exception):
2122
"""
2223

2324

25+
@dataclass
2426
class NoSnapshotError(StagpyError):
25-
"""Raised when no snapshot can be found.
27+
"""Raised when no snapshot can be found."""
2628

27-
Attributes:
28-
sdat (StagyyData): the `StagyyData` for which no snapshot was found.
29-
"""
30-
31-
def __init__(self, sdat: StagyyData):
32-
self.sdat = sdat
33-
super().__init__(f"no snapshot found for {sdat}")
29+
sdat: StagyyData
3430

3531

32+
@dataclass
3633
class NoGeomError(StagpyError):
37-
"""Raised when no geometry info can be found.
38-
39-
Attributes:
40-
step (Step): the `Step` for which no geometry was found.
41-
"""
34+
"""Raised when no geometry info can be found."""
4235

43-
def __init__(self, step: Step):
44-
self.step = step
45-
super().__init__(f"no geometry info found for {step!r}")
36+
step: Step
4637

4738

39+
@dataclass
4840
class NoTimeError(StagpyError):
49-
"""Raised when no time can be found for a step.
50-
51-
Attributes:
52-
step (Step): the `Step` instance for which no geometry was found.
53-
"""
41+
"""Raised when no time can be found for a step."""
5442

55-
def __init__(self, step: Step):
56-
self.step = step
57-
super().__init__(f"no time found for {step!r}")
43+
step: Step
5844

5945

46+
@dataclass
6047
class NoRefstateError(StagpyError):
61-
"""Raised when no refstate output can be found.
48+
"""Raised when no refstate output can be found."""
6249

63-
Attributes:
64-
sdat (StagyyData): the `StagyyData` instance for which no refstate was found.
65-
"""
66-
67-
def __init__(self, sdat: StagyyData):
68-
self.sdat = sdat
69-
super().__init__(f"no refstate found for {sdat!r}")
50+
sdat: StagyyData
7051

7152

53+
@dataclass
7254
class NoParFileError(StagpyError):
73-
"""Raised when no par file can be found.
74-
75-
Attributes:
76-
parfile (PathLike): the expected path of the par file.
77-
"""
55+
"""Raised when no par file can be found."""
7856

79-
def __init__(self, parfile: Path):
80-
self.parfile = parfile
81-
super().__init__(f"{parfile} file not found")
57+
parfile: Path
8258

8359

8460
class NotAvailableError(StagpyError):
8561
"""Raised when a feature is not available yet."""
8662

8763

64+
@dataclass
8865
class ParsingError(StagpyError):
89-
"""Raised when a parsing error occurs.
90-
91-
Attributes:
92-
file (PathLike): path of the file where a parsing problem was encountered.
93-
msg (str): error message.
94-
"""
66+
"""Raised when a parsing error occurs."""
9567

96-
def __init__(self, file: Path, msg: str):
97-
self.file = file
98-
self.msg = msg
99-
super().__init__(file, msg)
68+
file: Path
69+
msg: str
10070

10171

72+
@dataclass
10273
class InvalidTimestepError(StagpyError):
103-
"""Raised when invalid time step is requested.
74+
"""Raised when invalid time step is requested."""
10475

105-
Attributes:
106-
sdat (StagyyData): the `StagyyData` instance to which the request was made.
107-
istep (int): the invalid time step index.
108-
msg (str): the error message.
109-
"""
110-
111-
def __init__(self, sdat: StagyyData, istep: int, msg: str):
112-
self.sdat = sdat
113-
self.istep = istep
114-
self.msg = msg
115-
super().__init__(sdat, istep, msg)
76+
sdat: StagyyData
77+
istep: int
78+
msg: str
11679

11780

81+
@dataclass
11882
class InvalidSnapshotError(StagpyError):
119-
"""Raised when invalid snapshot is requested.
120-
121-
Attributes:
122-
sdat (StagyyData): the `StagyyData` instance to which the request was made.
123-
isnap (int): the invalid snapshot index.
124-
msg (str): the error message.
125-
"""
83+
"""Raised when invalid snapshot is requested."""
12684

127-
def __init__(self, sdat: StagyyData, isnap: int, msg: str):
128-
self.sdat = sdat
129-
self.isnap = isnap
130-
self.msg = msg
131-
super().__init__(sdat, isnap, msg)
85+
sdat: StagyyData
86+
isnap: int
87+
msg: str
13288

13389

90+
@dataclass
13491
class InvalidTimeFractionError(StagpyError):
135-
"""Raised when invalid fraction of series is requested.
136-
137-
Attributes:
138-
fraction (float): the invalid fraction.
139-
"""
92+
"""Raised when invalid fraction of series is requested, should be in (0, 1]."""
14093

141-
def __init__(self, fraction: float):
142-
self.fraction = fraction
143-
super().__init__(f"Fraction should be in (0,1] (received {fraction})")
94+
fraction: float
14495

14596

97+
@dataclass
14698
class InvalidNfieldsError(StagpyError):
147-
"""Raised when invalid nfields_max is requested.
99+
"""Raised when invalid nfields_max is requested."""
148100

149-
Attributes:
150-
nfields (int): the invalid number of field.
151-
"""
152-
153-
def __init__(self, nfields: int):
154-
self.nfields = nfields
155-
super().__init__(f"nfields_max should be >5 (received {nfields})")
101+
nfields: int
156102

157103

104+
@dataclass
158105
class InvalidZoomError(StagpyError):
159-
"""Raised when invalid zoom is requested.
160-
161-
Attributes:
162-
zoom (float): the invalid zoom level.
163-
"""
106+
"""Raised when invalid zoom is requested, should be in [0, 360]."""
164107

165-
def __init__(self, zoom: float):
166-
self.zoom = zoom
167-
super().__init__(f"Zoom angle should be in [0,360] (received {zoom})")
108+
zoom: float
168109

169110

170111
class MissingDataError(StagpyError):
171112
"""Raised when requested data is not present in output."""
172113

173114

115+
@dataclass
174116
class UnknownVarError(StagpyError):
175-
"""Raised when invalid var is requested.
176-
177-
Attributes:
178-
varname (str): the invalid var name.
179-
"""
117+
"""Raised when invalid var is requested."""
180118

181-
def __init__(self, varname: str):
182-
self.varname = varname
183-
super().__init__(varname)
119+
varname: str
184120

185121

186122
class UnknownFieldVarError(UnknownVarError):

0 commit comments

Comments
 (0)