Repository navigation
all: Modernize error wrapping - #8491
Conversation
This replaces old style errors.Wrap with modern fmt.Errorf and removes the (direct) dependency on github.com/pkg/errors. A couple of cases are adjusted by hand as previously errors.Wrap(nil, ...) would return nil, which is not what fmt.Errorf does.
|
Currently short on spare time, hopefully I can take a look at this over the weekend. |
| if f.Type != config.FolderTypeReceiveEncrypted { | ||
| if err := f.ignores.Load(".stignore"); err != nil && !fs.IsNotExist(err) { | ||
| return errors.Wrap(err, "loading ignores") | ||
| return fmt.Errorf("loading ignores: %w", err) |
There was a problem hiding this comment.
Should this and others be changed from loading to simply load to be consistent?
There was a problem hiding this comment.
While we're at it, yeah I'd prefer that.
EDIT: Just saw that the "...ing" style is used in many many places... So we don't have a consistent baseline and the change would be rather big. Not sure it's worth the trouble.
There was a problem hiding this comment.
Yeah, no, this is about the way errors are generated. I'd welcome a review of returned errors (and have a number of changes queued that add additional context strings), but this PR is not that.
acolomb
left a comment
There was a problem hiding this comment.
Looks good in general. Some tidying up we could do along the way suggested.
| if f.Type != config.FolderTypeReceiveEncrypted { | ||
| if err := f.ignores.Load(".stignore"); err != nil && !fs.IsNotExist(err) { | ||
| return errors.Wrap(err, "loading ignores") | ||
| return fmt.Errorf("loading ignores: %w", err) |
There was a problem hiding this comment.
| return fmt.Errorf("loading ignores: %w", err) | |
| return fmt.Errorf("load ignores: %w", err) |
| curFile, hasCurFile := snap.Get(protocol.LocalDeviceID, file.Name) | ||
| if err := f.scanIfItemChanged(file.Name, info, curFile, hasCurFile, scanChan); err != nil { | ||
| err = errors.Wrap(err, "handling dir") | ||
| err = fmt.Errorf("handling dir: %w", err) |
There was a problem hiding this comment.
| err = fmt.Errorf("handling dir: %w", err) | |
| err = fmt.Errorf("handle dir: %w", err) |
| dbUpdateChan <- dbUpdateJob{file, dbUpdateHandleDir} | ||
| } else { | ||
| f.newPullError(file.Name, errors.Wrap(err, "creating directory")) | ||
| f.newPullError(file.Name, fmt.Errorf("creating directory: %w", err)) |
There was a problem hiding this comment.
| f.newPullError(file.Name, fmt.Errorf("creating directory: %w", err)) | |
| f.newPullError(file.Name, fmt.Errorf("create directory: %w", err)) |
| // anything else with it if we can't even stat() it. | ||
| case err != nil: | ||
| f.newPullError(file.Name, errors.Wrap(err, "checking file to be replaced")) | ||
| f.newPullError(file.Name, fmt.Errorf("checking file to be replaced: %w", err)) |
There was a problem hiding this comment.
| f.newPullError(file.Name, fmt.Errorf("checking file to be replaced: %w", err)) | |
| f.newPullError(file.Name, fmt.Errorf("check file to be replaced: %w", err)) |
| @@ -647,13 +647,13 @@ func (f *sendReceiveFolder) handleDir(file protocol.FileInfo, snap *db.Snapshot, | |||
| if err = f.inWritableDir(mkdir, file.Name); err == nil { | |||
There was a problem hiding this comment.
Invert to err != nil for consistency?
| fd, err := s.fs.OpenFile(s.tempName, flags, mode) | ||
| if err != nil { | ||
| return errors.Wrap(err, "opening temp file") | ||
| return fmt.Errorf("opening temp file: %w", err) |
There was a problem hiding this comment.
| return fmt.Errorf("opening temp file: %w", err) | |
| return fmt.Errorf("open temp file: %w", err) |
| cfg, err = DefaultConfig(path, myID, evLogger, noDefaultFolder, skipPortProbing) | ||
| if err != nil { | ||
| return nil, errors.Wrap(err, "failed to generate default config") | ||
| return nil, fmt.Errorf("failed to generate default config: %w", err) |
There was a problem hiding this comment.
| return nil, fmt.Errorf("failed to generate default config: %w", err) | |
| return nil, fmt.Errorf("generate default config: %w", err) |
| err = cfg.Save() | ||
| if err != nil { | ||
| return nil, errors.Wrap(err, "failed to save default config") | ||
| return nil, fmt.Errorf("failed to save default config: %w", err) |
There was a problem hiding this comment.
| return nil, fmt.Errorf("failed to save default config: %w", err) | |
| return nil, fmt.Errorf("save default config: %w", err) |
| return nil, errors.New("failed to load config: unexpected end of file. Truncated or empty configuration?") | ||
| } else if err != nil { | ||
| return nil, errors.Wrap(err, "failed to load config") | ||
| return nil, fmt.Errorf("failed to load config: %w", err) |
There was a problem hiding this comment.
| return nil, fmt.Errorf("failed to load config: %w", err) | |
| return nil, fmt.Errorf("load config: %w", err) |
| } | ||
| l.Infof("Default config saved. Edit %s to taste (with Syncthing stopped) or use the GUI", cfg.ConfigPath()) | ||
| } else if err == io.EOF { | ||
| return nil, errors.New("failed to load config: unexpected end of file. Truncated or empty configuration?") |
There was a problem hiding this comment.
| return nil, errors.New("failed to load config: unexpected end of file. Truncated or empty configuration?") | |
| return nil, errors.New("load config: unexpected end of file. Truncated or empty configuration?") |
|
Thanks for the review and catching the one place I apparently missed an old Wrap()... But I really don't want to "clean up" all the error strings or invert logic in places etc; this is about making a straight change to a modern API. If we want to refactor all error strings that's fine, but it's a separate project and certainly should affect places that weren't errors.Wrap previously.. |
* main: cmd/syncthing: Use os.executable in monitor, only fallback to PATH (syncthing#8502) all: Modernize error wrapping (syncthing#8491) gui, man, authors: Update docs, translations, and contributors cmd/syncthing: Work around binary in current dir restriction (fixes syncthing#8499) (syncthing#8500) lib/fs: Cache user lookups (syncthing#8496) all: Make scanning ownership opt-in (syncthing#8497)
* main: (256 commits) cmd/syncthing: Use os.executable in monitor, only fallback to PATH (syncthing#8502) all: Modernize error wrapping (syncthing#8491) gui, man, authors: Update docs, translations, and contributors cmd/syncthing: Work around binary in current dir restriction (fixes syncthing#8499) (syncthing#8500) lib/fs: Cache user lookups (syncthing#8496) all: Make scanning ownership opt-in (syncthing#8497) lib/model: Skip paused folders in aggregated completion loop (fixes syncthing#8219) (syncthing#8220) gui: Remove unused strings from translations (syncthing#8288) gui, api: Show internal config and state paths (fixes syncthing#8323) (syncthing#8324) build(deps): bump github.com/go-ldap/ldap/v3 from 3.4.1 to 3.4.4 (syncthing#8475) build: bump actions/setup-go from 2.2.0 to 3.2.1 (syncthing#8492) build: Let dependabot suggest updates to GitHub actions (syncthing#8490) update: Go version in update-docs-translations action (syncthing#8489) gui, man, authors: Update docs, translations, and contributors cmd/syncthing: Refactor CLI code to allow passing CLI arguments via function parameter (syncthing#8485) build: Update quic-go for Go 1.19 (syncthing#8483) lib/ur: Properly initialize map in failure data (fixes syncthing#8479) (syncthing#8480) lib/ur: Properly initialize map in failure data (fixes syncthing#8479) gui, man, authors: Update docs, translations, and contributors docs: Clarify docker network mode limitations (syncthing#8472) ...
This replaces old style errors.Wrap with modern fmt.Errorf and removes
the (direct) dependency on github.com/pkg/errors. A couple of cases are
adjusted by hand as previously errors.Wrap(nil, ...) would return nil,
which is not what fmt.Errorf does.
Existing calls to errors.New, errors.Is and errors.As work the same as previously.