From 94b9157d880cea03ba151b010306857707c31001 Mon Sep 17 00:00:00 2001 From: wgroeneveld Date: Wed, 8 Mar 2023 21:12:15 +0100 Subject: [PATCH] refactor errors to use Go 1.13 errors.Is --- main.go | 6 +++--- restic/wrapper.go | 18 +++++------------- restic/wrapper_test.go | 4 ++-- 3 files changed, 10 insertions(+), 18 deletions(-) diff --git a/main.go b/main.go index ba9753c..d0279fe 100644 --- a/main.go +++ b/main.go @@ -2,6 +2,7 @@ package main import ( "brainbaking.com/restictray/restic" + "errors" "fyne.io/systray" "github.com/rs/zerolog" "github.com/rs/zerolog/log" @@ -66,9 +67,8 @@ func main() { func updateSnapshots(cnf *restic.Config, mnu *resticmenu) { err := wrapper.UpdateLatestSnapshots(cnf) if err != nil { - te, ok := err.(*restic.ResticCmdTimeoutError) - if ok { - log.Warn().Err(te).Msg("update snapshot timeout") + if errors.Is(err, restic.ResticCmdTimeoutError) { + log.Warn().Err(err).Msg("update snapshot timeout") mnu.latestSnapshot.SetTitle("‼️ Latest: timeout while updating") mnu.backupNowEnable() } else { diff --git a/restic/wrapper.go b/restic/wrapper.go index bcaf605..04a5fd2 100644 --- a/restic/wrapper.go +++ b/restic/wrapper.go @@ -27,13 +27,7 @@ type Wrapper struct { mountCommand *exec.Cmd } -type ResticCmdTimeoutError struct { - KillErr error -} - -func (r *ResticCmdTimeoutError) Error() string { - return fmt.Errorf("backup output timeout, repository server down?: %w", r.KillErr).Error() -} +var ResticCmdTimeoutError = errors.New("backup output timeout, repository server down?") func (w *Wrapper) HasSnapshots() bool { return len(w.LatestSnapshots) > 0 @@ -130,9 +124,8 @@ func (w *Wrapper) UpdateLatestSnapshots(c *Config) error { select { case <-done: case <-time.After(cmdTimeout): - return &ResticCmdTimeoutError{ - KillErr: cmd.Process.Kill(), - } + cmd.Process.Kill() + return fmt.Errorf("restic snapshots cmd: %w", ResticCmdTimeoutError) } if err != nil { @@ -173,9 +166,8 @@ func (w *Wrapper) Backup(c *Config) error { case <-busy: busy = nil // It's alive! continue with cmd.Wait() case <-time.After(cmdTimeout): - return &ResticCmdTimeoutError{ - KillErr: cmd.Process.Kill(), - } + cmd.Process.Kill() + return fmt.Errorf("restic snapshots cmd: %w", ResticCmdTimeoutError) } err := cmd.Wait() diff --git a/restic/wrapper_test.go b/restic/wrapper_test.go index c01ebc8..58d9918 100644 --- a/restic/wrapper_test.go +++ b/restic/wrapper_test.go @@ -189,7 +189,7 @@ func TestBackup_TakesLongerThanNeededToOutput_TimesOut(t *testing.T) { wrapper := Wrapper{} err := wrapper.Backup(&Config{}) - assert.ErrorContainsf(t, err, "backup output timeout", "timeout expected") + assert.ErrorIs(t, err, ResticCmdTimeoutError) } func TestUpdateLatestSnapshot_TakesLongerThanNeededToOutput_TimesOut(t *testing.T) { @@ -200,7 +200,7 @@ func TestUpdateLatestSnapshot_TakesLongerThanNeededToOutput_TimesOut(t *testing. err := wrapper.UpdateLatestSnapshots(&Config{}) assert.Empty(t, wrapper.LatestSnapshots) - assert.ErrorContainsf(t, err, "backup output timeout", "timeout expected") + assert.ErrorIs(t, err, ResticCmdTimeoutError) } func TestUpdateLatestSnapshots_FromJSONOutputOfRestic(t *testing.T) {