Return a wrapped fs.ErrNotExist if no spec directories are defined#309
Return a wrapped fs.ErrNotExist if no spec directories are defined#309elezar wants to merge 1 commit intocncf-tags:mainfrom
Conversation
|
Thanks @klihub. While working on this, I was wondering whether we want to more strictly define (i.e. return an error from |
Yeah, at least my gut feeling is that it sounds like a reasonable idea. Another option would be to update and document the |
I think this is a cleaner solution. Created #310 to do this. |
2026e9b to
bb2fc73
Compare
|
rebased. |
| specDir, _ = c.highestPrioritySpecDir() | ||
| if specDir == "" { | ||
| return errors.New("no Spec directories to remove from") | ||
| return fmt.Errorf("no Spec directories defined: %w", fs.ErrNotExist) |
There was a problem hiding this comment.
the fix looks good to me, but how about adding test case for this?
err := cache.RemoveSpec("something")
assert.True(t, errors.Is(err, fs.ErrNotExist))There was a problem hiding this comment.
I have added a basic test.
There was a problem hiding this comment.
@klihub as also pointed out by the test, we do have a slight disconnect here. In the case where the spec dirs are empty, we return ErrNotExist, but in the case where os.Remove fails for the spec we are trying to remove due to ErrNotExist we return nil. Should we always return nil (or ErrNotExist) for consistency?
There was a problem hiding this comment.
@elezar Yes, probably would make sense. Returning ErrNotExist as such would provide transparency for RemoveSpec() while returnin nil for ErrNotExist would provide idempotency. From the top of my head I don't know which would be better.
There was a problem hiding this comment.
I think at this stage switching to returning ErrNotExists will end up breaking producers that use this code. I'm not sure if that's something we want to do. Let me add a comment to the code to this effect.
Also, as a follow-up, does this mean that we should rather return nil if there are no spec directories (especially given that this is now a case that we don't expect to ever occur)?
There was a problem hiding this comment.
Also, as a follow-up, does this mean that we should rather return nil if there are no spec directories (especially given that this is now a case that we don't expect to ever occur)?
On the RemoveSpec() code path I think we could return nil if we have no spec directories if we return nil when there is no spec file to remove, as the former implies the latter.
I think we still might get into the situation when there are no spec dirs, because someone might nuke them while we are running.
There was a problem hiding this comment.
I think at this stage switching to returning ErrNotExists will end up breaking producers that use this code. I'm not sure if that's something we want to do. Let me add a comment to the code to this effect.
Yes, I also think we should try to be extra conservative even when it feels like PITA. We're post 1.0, so we should not be making breaking changes. Even if it's not immediately obvious why and how someone could rely on existing functionality so that it would break if we start returning ErrNotExists, it's better not to risk especially if there is not much to gain by the change.
| @@ -321,7 +321,7 @@ func (c *Cache) RemoveSpec(name string) error { | |||
|
|
|||
| specDir, _ = c.highestPrioritySpecDir() | |||
| if specDir == "" { | |||
There was a problem hiding this comment.
It shouldn't be possible, no.
There was a problem hiding this comment.
Does that mean we want to update the highestPrioritySpecDir implementation to not return an error?
There was a problem hiding this comment.
I think it does not return an error. It returns a directory and its priority.
Signed-off-by: Evan Lezar <elezar@nvidia.com>
bb2fc73 to
092f1f9
Compare
See the discussion here: #308 (comment)
Here we return a wrapped
fs.ErrNotExisterror if there are no spec directories defined instead of a raw error that cannot be queried easily by the caller.