-
Notifications
You must be signed in to change notification settings - Fork 18k
filepath.Walk is error-prone when handling errors #2237
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
Labels
Comments
Owner changed to @griesemer. Status changed to Accepted. |
I see two obvious approaches to fix this: a) Expand the Visitor with an error handler, and simplify the Walk signature: type Visitor interface { VisitDir(path string, f *os.FileInfo) bool VisitFile(path string, f *os.FileInfo) Error(os.Error) } func Walk(root string, v Visitor) b) Instead of a channel, provide an error handler to Walk and leave the Visitor as is: func Walk(root string, v Visitor, error func(os.Error)) Discussion: a) has the advantage of more naturally integrating any kind of error handling done by a visitor's implementation, but it requires all visitors to implement an Error method. b) is simply an alternative to what we have now (and existing implementations can be easily adjusted by providing a function/closure that sends on an error channel as error handler. Looking for feedback. Status changed to WaitingForReply. |
I'd personally prefer (a), because it enables the visitor itself to use the same error handling mechanism by calling v.Error within VisitDir and/or VisitFile if wanted. I also suggest we use the signature of Error(path string, os.Error) instead, to make the interface even and also to enable the erroring logic to use the path if desired, but that's minor. |
Also, just for completeness, there is a third option which is a variation of (a): c) Use an independent ErrorHandler interface to determine if the visitor wants to handle errors or not. I think (a) is still cleaner, though, as it is an explicit acknowledgement from the developer that errors are being ignored. |
Candidate CL: http://golang.org/cl/4964067 |
This issue was closed by revision e5c20dc. Status changed to Fixed. |
This issue was closed by revision 49bcc88. |
Issue re-opened (fix was rolled back (https://code.google.com/p/go/source/detail?r=678116cefc3d8068dfc615ee70ff4e0cea139446 ). Status changed to Accepted. |
In case someone faces this issue before the new interface is sorted, the attached file is a drop-in replacement for filepath.Walk implementing the suggested API, using option (c) above. It will detect if the visitor is an error handler or not, so that the standard filepath.Visitor API continues to work with existing code. Attachments:
|
This issue was closed by revision 4e3b725. Status changed to Fixed. |
This issue was closed.
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
The text was updated successfully, but these errors were encountered: