Skip to content

Commit 19d3df2

Browse files
committed
Add new exception GMTParameterError for missing required parameters
1 parent d023f6e commit 19d3df2

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed

doc/api/index.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ All custom exceptions are derived from :class:`pygmt.exceptions.GMTError`.
286286
exceptions.GMTTypeError
287287
exceptions.GMTValueError
288288
exceptions.GMTTypeError
289-
289+
exceptions.GMTParameterError
290290

291291
.. currentmodule:: pygmt
292292

pygmt/exceptions.py

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
All exceptions derive from GMTError.
55
"""
66

7-
from collections.abc import Iterable
7+
from collections.abc import Iterable, Set
88
from typing import Any
99

1010

@@ -130,3 +130,40 @@ def __init__(self, dtype: object, /, reason: str | None = None):
130130
if reason:
131131
msg += f" {reason}"
132132
super().__init__(msg)
133+
134+
135+
class GMTParameterError(GMTError):
136+
"""
137+
Raised when parameters are missing or invalid.
138+
139+
Parameters
140+
----------
141+
required
142+
Names of required parameters.
143+
exclusive
144+
Names of mutually exclusive parameters.
145+
reason
146+
Detailed reason why the parameters are invalid.
147+
"""
148+
149+
def __init__(
150+
self,
151+
*,
152+
required: Set[str] | None = None,
153+
exclusive: Set[str] | None = None,
154+
reason: str | None = None,
155+
):
156+
msg = ""
157+
if required:
158+
msg = (
159+
"Required parameter(s) are missing: "
160+
f"{', '.join(repr(par) for par in required)}."
161+
)
162+
if exclusive:
163+
msg = (
164+
"Mutually exclusive parameter(s) are specified: "
165+
f"{', '.join(repr(par) for par in exclusive)}."
166+
)
167+
if reason:
168+
msg += f" {reason}"
169+
super().__init__(msg)

0 commit comments

Comments
 (0)