refactor errors to use Go 1.13 errors.Is

This commit is contained in:
Wouter Groeneveld 2023-03-08 21:12:15 +01:00
parent ec12985ee7
commit 94b9157d88
3 changed files with 10 additions and 18 deletions

View File

@ -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 {

View File

@ -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()

View File

@ -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) {