Skip to content

Commit 8fe6a13

Browse files
committed
Add GMTParameterError and deprecate GMTInvalidInput
1 parent 3062417 commit 8fe6a13

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

doc/api/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,7 @@ All custom exceptions are derived from :class:`pygmt.exceptions.GMTError`.
294294

295295
exceptions.GMTError
296296
exceptions.GMTInvalidInput
297+
exceptions.GMTParameterError
297298
exceptions.GMTVersionError
298299
exceptions.GMTOSError
299300
exceptions.GMTCLibError

pygmt/exceptions.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,55 @@ class GMTCLibNoSessionError(GMTCLibError):
4141
class GMTInvalidInput(GMTError): # noqa: N818
4242
"""
4343
Raised when the input of a function/method is invalid.
44+
45+
.. deprecated:: 0.13.0
46+
Use :class:`GMTParameterError` instead.
47+
"""
48+
49+
50+
class GMTParameterError(GMTError): # noqa: N818
4451
"""
52+
Raised when parameters are missing or invalid.
53+
54+
Parameters
55+
----------
56+
required
57+
Names of required parameters.
58+
require_any
59+
Names of parameters where at least one must be specified.
60+
exclusive
61+
Names of mutually exclusive parameters.
62+
reason
63+
Detailed reason why the parameters are invalid.
64+
"""
65+
66+
def __init__(
67+
self,
68+
*,
69+
required: set[str] | None = None,
70+
require_any: set[str] | None = None,
71+
exclusive: set[str] | None = None,
72+
reason: str | None = None,
73+
):
74+
msg = []
75+
if required:
76+
msg.append(
77+
"Required parameter(s) are missing: "
78+
f"{', '.join(repr(par) for par in required)}."
79+
)
80+
if require_any:
81+
msg.append(
82+
"At least one of the following parameters must be specified: "
83+
f"{', '.join(repr(par) for par in require_any)}."
84+
)
85+
if exclusive:
86+
msg.append(
87+
"Mutually exclusive parameter(s) are specified: "
88+
f"{', '.join(repr(par) for par in exclusive)}."
89+
)
90+
if reason:
91+
msg.append(reason)
92+
super().__init__(" ".join(msg))
4593

4694

4795
class GMTVersionError(GMTError):

0 commit comments

Comments
 (0)