Base return types for generator errors #154
Closed
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There are a couple methods in the puppetdb API which return empty lists instead of 404s when querying an invalid value, two of these being queries for a particular fact name of a node (/pdb/query/v4/nodes//facts/), or a resource type/name (/pdb/query/v4/resources//<TITLE>)
In the types objects for pypuppetdb, these two queries used a return next(x for x in y) type pattern, however since the API returns an empty list, the implied generator starts out empty, and instead of returning a valid object, raises a StopIteration, which is not handled by pypuppetdb.
For the end user, this is extremely confusing, as a StopIteration doesn't behave like a standard exception. I've added in a default case to the next() method to return the empty list in case of an invalid result, which mimics the behavior of the puppetdb API.
While I have managed to manually test these changes against my own puppet infrastructure, there doesn't seem to be an easy way for me to add unit tests for the changes given the current pytest fixtures available, as the Node.fact() method relies on the underlying baseapi.