24
24
)
25
25
from pygmt .clib .loading import get_gmt_version , load_libgmt
26
26
from pygmt .datatypes import _GMT_DATASET , _GMT_GRID , _GMT_IMAGE
27
- from pygmt .exceptions import GMTCLibError , GMTCLibNoSessionError , GMTInvalidInput
27
+ from pygmt .exceptions import (
28
+ GMTCLibError ,
29
+ GMTCLibNoSessionError ,
30
+ GMTInvalidInput ,
31
+ GMTValueError ,
32
+ )
28
33
from pygmt .helpers import (
29
34
_validate_data_input ,
30
35
data_kind ,
@@ -560,11 +565,13 @@ def get_common(self, option: str) -> bool | int | float | np.ndarray:
560
565
... lib.get_common("A")
561
566
Traceback (most recent call last):
562
567
...
563
- pygmt.exceptions.GMTInvalidInput: Unknown GMT common option flag 'A'.
568
+ pygmt.exceptions.GMTValueError: Invalid GMT common option: 'A'. Expected .. .
564
569
"""
565
- if option not in "BIJRUVXYabfghinoprst:" :
566
- msg = f"Unknown GMT common option flag '{ option } '."
567
- raise GMTInvalidInput (msg )
570
+ valid_options = "BIJRUVXYabfghinoprst:"
571
+ if option not in valid_options :
572
+ raise GMTValueError (
573
+ option , description = "GMT common option" , choices = valid_options
574
+ )
568
575
569
576
c_get_common = self .get_libgmt_func (
570
577
"GMT_Get_Common" ,
@@ -847,15 +854,15 @@ def _parse_constant(
847
854
their values are added.
848
855
849
856
If no valid modifiers are given, then will assume that modifiers are not
850
- allowed. In this case, will raise a :class:`pygmt.exceptions.GMTInvalidInput `
857
+ allowed. In this case, will raise a :class:`pygmt.exceptions.GMTValueError `
851
858
exception if given a modifier.
852
859
853
860
Parameters
854
861
----------
855
862
constant
856
863
The name of a valid GMT API constant, with an optional modifier.
857
864
valid
858
- A list of valid values for the constant. Will raise a GMTInvalidInput
865
+ A list of valid values for the constant. Will raise a GMTValueError
859
866
exception if the given value is not in the list.
860
867
valid_modifiers
861
868
A list of valid modifiers that can be added to the constant. If ``None``,
@@ -866,28 +873,23 @@ def _parse_constant(
866
873
nmodifiers = len (parts ) - 1
867
874
868
875
if name not in valid :
869
- msg = f"Invalid constant name '{ name } '. Must be one of { valid } ."
870
- raise GMTInvalidInput (msg )
876
+ raise GMTValueError (name , description = "constant name" , choices = valid )
871
877
872
878
match nmodifiers :
873
879
case 1 if valid_modifiers is None :
874
- msg = (
875
- f"Constant modifiers are not allowed since valid values "
876
- f" were not given: ' { constant } '."
880
+ raise GMTValueError (
881
+ constant ,
882
+ reason = "Constant modifiers are not allowed since valid values were not given." ,
877
883
)
878
- raise GMTInvalidInput (msg )
879
884
case 1 if valid_modifiers is not None and parts [1 ] not in valid_modifiers :
880
- msg = (
881
- f"Invalid constant modifier '{ parts [1 ]} '. "
882
- f"Must be one of { valid_modifiers } ."
885
+ raise GMTValueError (
886
+ parts [1 ], description = "constant modifier" , choices = valid_modifiers
883
887
)
884
- raise GMTInvalidInput (msg )
885
888
case n if n > 1 :
886
- msg = (
887
- f"Only one modifier is allowed in constants, "
888
- f" { nmodifiers } given: ' { constant } '."
889
+ raise GMTValueError (
890
+ constant ,
891
+ reason = f"Only one modifier is allowed in constants but { nmodifiers } given." ,
889
892
)
890
- raise GMTInvalidInput (msg )
891
893
892
894
integer_value = sum (self [part ] for part in parts )
893
895
return integer_value
0 commit comments