Skip to content

all: Modernize error wrapping - #8491

Merged
calmh merged 3 commits into
syncthing:mainfrom
calmh:wraperrors
Aug 16, 2022
Merged

calmh merged 3 commits into
syncthing:mainfrom
calmh:wraperrors

Conversation

@calmh

@calmh calmh commented Aug 9, 2022 •

Copy link
Copy Markdown
Member

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.

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.
@calmh
calmh requested a review from acolomb August 12, 2022 05:49
@acolomb

acolomb commented Aug 12, 2022

Copy link
Copy Markdown
Member

Currently short on spare time, hopefully I can take a look at this over the weekend.

Comment thread lib/model/folder.go
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)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this and others be changed from loading to simply load to be consistent?

@acolomb acolomb Aug 14, 2022 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 acolomb left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good in general. Some tidying up we could do along the way suggested.

Comment thread lib/model/folder.go
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)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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 {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return fmt.Errorf("opening temp file: %w", err)
return fmt.Errorf("open temp file: %w", err)

Comment thread lib/syncthing/utils.go
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)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return nil, fmt.Errorf("failed to generate default config: %w", err)
return nil, fmt.Errorf("generate default config: %w", err)

Comment thread lib/syncthing/utils.go
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)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return nil, fmt.Errorf("failed to save default config: %w", err)
return nil, fmt.Errorf("save default config: %w", err)

Comment thread lib/syncthing/utils.go
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)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return nil, fmt.Errorf("failed to load config: %w", err)
return nil, fmt.Errorf("load config: %w", err)

Comment thread lib/syncthing/utils.go
}
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?")

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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?")

@calmh

calmh commented Aug 15, 2022 •

Copy link
Copy Markdown
Member Author

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..

@calmh
calmh merged commit b10d106 into syncthing:main Aug 16, 2022
@calmh
calmh deleted the wraperrors branch August 16, 2022 09:11
calmh added a commit to calmh/syncthing that referenced this pull request Aug 17, 2022
* 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)
calmh added a commit to calmh/syncthing that referenced this pull request Aug 18, 2022
* 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)
  ...
@calmh calmh added this to the v1.21.0 milestone Aug 23, 2022
@st-review st-review added the frozen-due-to-age Issues closed and untouched for a long time, together with being locked for discussion label Aug 16, 2023
@syncthing syncthing locked and limited conversation to collaborators Aug 16, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

frozen-due-to-age Issues closed and untouched for a long time, together with being locked for discussion

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants