Skip to content

Commit

Permalink
Raise the correct exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
shahramn committed Dec 15, 2023
1 parent 5b3367e commit 2f48b83
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 7 deletions.
2 changes: 1 addition & 1 deletion gribapi/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ class InvalidIndexError(GribInternalError):


class InvalidGribError(GribInternalError):
"""Invalid grib id."""
"""Invalid GRIB id."""


class InvalidFileError(GribInternalError):
Expand Down
8 changes: 4 additions & 4 deletions gribapi/gribapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,13 +162,13 @@ def wrapper(*args):
def get_handle(msgid):
h = ffi.cast("grib_handle*", msgid)
if h == ffi.NULL:
raise errors.InvalidGribError(f"get_handle: Bad message ID {msgid}")
raise errors.NullHandleError(f"get_handle: Bad message ID {msgid}")
return h


def put_handle(handle):
if handle == ffi.NULL:
raise errors.InvalidGribError(f"put_handle: Bad message ID {handle}")
raise errors.NullHandleError(f"put_handle: Bad message ID {handle}")
return int(ffi.cast("size_t", handle))


Expand Down Expand Up @@ -1137,7 +1137,7 @@ def grib_clone(msgid_src):
h_src = get_handle(msgid_src)
h_dest = lib.grib_handle_clone(h_src)
if h_dest == ffi.NULL:
raise errors.InvalidGribError("clone failed")
raise errors.MessageInvalidError("clone failed")
return put_handle(h_dest)


Expand Down Expand Up @@ -2381,7 +2381,7 @@ def grib_new_from_message(message):
message = message.encode(ENC)
h = lib.grib_handle_new_from_message_copy(ffi.NULL, message, len(message))
if h == ffi.NULL:
raise errors.InvalidGribError("new_from_message failed")
raise errors.MessageInvalidError("new_from_message failed")
return put_handle(h)


Expand Down
4 changes: 2 additions & 2 deletions tests/test_eccodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ def test_codes_get_native_type():
assert eccodes.codes_get_native_type(gid, "referenceValue") is float
assert eccodes.codes_get_native_type(gid, "stepType") is str
assert eccodes.codes_get_native_type(gid, "section_1") is None
with pytest.raises(eccodes.InvalidGribError):
eccodes.codes_get_native_type(0, "aKey")
with pytest.raises(eccodes.NullHandleError):
eccodes.codes_get_native_type(0, "aKey") # NULL handle


def test_new_from_file():
Expand Down

0 comments on commit 2f48b83

Please sign in to comment.