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

Try to improve error messages when creating Variables #1268

Merged
merged 3 commits into from
Aug 31, 2023

Conversation

ZedThree
Copy link
Contributor

@ZedThree ZedThree commented Aug 2, 2023

Error messages are currently reported directly from the netcdf-C error string, which doesn't include information about the specific object. This can make debugging awkward or painful. This PR simply includes the variable and group names in error messages arising from Variable.__init__.

Minimal example:

import netCDF4

with netCDF4.Dataset("badname.nc", "w", format="NETCDF4") as f:
    time = f.createDimension("time", None)
    g = f.createVariable("badname ", "i4", ("time",))

Error without this PR:

RuntimeError: NetCDF: Name contains illegal characters

With this PR:

RuntimeError: NetCDF: Name contains illegal characters: (variable 'badname ', group '/')

Exact format could be tweaked and used in more places.

@ZedThree
Copy link
Contributor Author

ZedThree commented Aug 2, 2023

Note that unfortunately this likely can't be solved in the netCDF-C library itself because the error strings are const char*, so I don't think there's a good way to include dynamic information in the error message.

@DennisHeimbigner
Copy link
Collaborator

The alternative is to log a more complete message before returning the error.

@jswhit
Copy link
Collaborator

jswhit commented Aug 25, 2023

looks good - I can merge once you update to the current master and include a Changelog entry

@jswhit jswhit merged commit 8729666 into Unidata:master Aug 31, 2023
29 checks passed
@ZedThree ZedThree deleted the better-error-messages branch August 31, 2023 08:19
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

Successfully merging this pull request may close these issues.

3 participants