Skip to content

Commit

Permalink
Better error message in case of a non GRIB file. Closes #34 for real.
Browse files Browse the repository at this point in the history
  • Loading branch information
alexamici committed Nov 30, 2018
1 parent 88b5269 commit 9389835
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
4 changes: 4 additions & 0 deletions cfgrib/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,10 +207,14 @@ class FileStream(collections.Iterable):
def __iter__(self):
# type: () -> T.Generator[Message, None, None]
with open(self.path, 'rb') as file:
valid_grib_message_found = False
while True:
try:
yield self.message_from_file(file, errors=self.errors)
valid_grib_message_found = True
except EOFError:
if not valid_grib_message_found:
raise EOFError("No valid GRIB message found in file: %r" % self.path)
break
except Exception:
if self.errors == 'ignore':
Expand Down
5 changes: 5 additions & 0 deletions tests/test_20_messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,3 +207,8 @@ def test_FileStream():
assert len(leader) > 100
assert sum(1 for _ in res) == leader['count']
assert len(res.index(['paramId'])) == 1

# __file__ is not a GRIB, but contains the "GRIB" string, so it is a very tricky corner case
res = messages.FileStream(str(__file__))
with pytest.raises(EOFError):
res.first()

0 comments on commit 9389835

Please sign in to comment.