Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

esgdrs: change "Invalid or corrupted NetCDF file" message if read permission is lacking #76

Open
alaniwi opened this issue Dec 1, 2020 · 1 comment

Comments

@alaniwi
Copy link
Collaborator

alaniwi commented Dec 1, 2020

If an input data file does not have read permission for the user running esgdrs, the message obtained is "Invalid or corrupted NetCDF file". It would be good to change this to something more informative.

@alaniwi
Copy link
Collaborator Author

alaniwi commented Dec 1, 2020

These are the places in the code.

def __enter__(self):
try:
self.nc = Dataset(self.path, self.mode)
except IOError:
raise InvalidNetCDFFile(self.path)
return self.nc

class InvalidNetCDFFile(Exception):
"""
Raised when invalid or corrupted NetCDF file.
"""
def __init__(self, path):
self.msg = "Invalid or corrupted NetCDF file."
self.msg += "\n<file: '{}'>".format(path)
super(self.__class__, self).__init__(self.msg)

It probably needs an additional custom exception type, and an explicit permissions test using os.access.

Correction: it might be possible to inspect the IOError exception for the error code, and if it is the one that corresponds to a permissions problem, then simply re-raise the IOError instead of the custom exception. Something like...

import errno

def __enter__(self): 
     try: 
         self.nc = Dataset(self.path, self.mode) 
     except IOError as exc: 
         if exc.errno == errno.EACCES:
             raise
         else:
             raise InvalidNetCDFFile(self.path) 
     return self.nc 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant