refactor errors to use Go 1.13 errors.Is
This commit is contained in:
parent
ec12985ee7
commit
94b9157d88
6
main.go
6
main.go
|
@ -2,6 +2,7 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"brainbaking.com/restictray/restic"
|
"brainbaking.com/restictray/restic"
|
||||||
|
"errors"
|
||||||
"fyne.io/systray"
|
"fyne.io/systray"
|
||||||
"github.com/rs/zerolog"
|
"github.com/rs/zerolog"
|
||||||
"github.com/rs/zerolog/log"
|
"github.com/rs/zerolog/log"
|
||||||
|
@ -66,9 +67,8 @@ func main() {
|
||||||
func updateSnapshots(cnf *restic.Config, mnu *resticmenu) {
|
func updateSnapshots(cnf *restic.Config, mnu *resticmenu) {
|
||||||
err := wrapper.UpdateLatestSnapshots(cnf)
|
err := wrapper.UpdateLatestSnapshots(cnf)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
te, ok := err.(*restic.ResticCmdTimeoutError)
|
if errors.Is(err, restic.ResticCmdTimeoutError) {
|
||||||
if ok {
|
log.Warn().Err(err).Msg("update snapshot timeout")
|
||||||
log.Warn().Err(te).Msg("update snapshot timeout")
|
|
||||||
mnu.latestSnapshot.SetTitle("‼️ Latest: timeout while updating")
|
mnu.latestSnapshot.SetTitle("‼️ Latest: timeout while updating")
|
||||||
mnu.backupNowEnable()
|
mnu.backupNowEnable()
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -27,13 +27,7 @@ type Wrapper struct {
|
||||||
mountCommand *exec.Cmd
|
mountCommand *exec.Cmd
|
||||||
}
|
}
|
||||||
|
|
||||||
type ResticCmdTimeoutError struct {
|
var ResticCmdTimeoutError = errors.New("backup output timeout, repository server down?")
|
||||||
KillErr error
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *ResticCmdTimeoutError) Error() string {
|
|
||||||
return fmt.Errorf("backup output timeout, repository server down?: %w", r.KillErr).Error()
|
|
||||||
}
|
|
||||||
|
|
||||||
func (w *Wrapper) HasSnapshots() bool {
|
func (w *Wrapper) HasSnapshots() bool {
|
||||||
return len(w.LatestSnapshots) > 0
|
return len(w.LatestSnapshots) > 0
|
||||||
|
@ -130,9 +124,8 @@ func (w *Wrapper) UpdateLatestSnapshots(c *Config) error {
|
||||||
select {
|
select {
|
||||||
case <-done:
|
case <-done:
|
||||||
case <-time.After(cmdTimeout):
|
case <-time.After(cmdTimeout):
|
||||||
return &ResticCmdTimeoutError{
|
cmd.Process.Kill()
|
||||||
KillErr: cmd.Process.Kill(),
|
return fmt.Errorf("restic snapshots cmd: %w", ResticCmdTimeoutError)
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -173,9 +166,8 @@ func (w *Wrapper) Backup(c *Config) error {
|
||||||
case <-busy:
|
case <-busy:
|
||||||
busy = nil // It's alive! continue with cmd.Wait()
|
busy = nil // It's alive! continue with cmd.Wait()
|
||||||
case <-time.After(cmdTimeout):
|
case <-time.After(cmdTimeout):
|
||||||
return &ResticCmdTimeoutError{
|
cmd.Process.Kill()
|
||||||
KillErr: cmd.Process.Kill(),
|
return fmt.Errorf("restic snapshots cmd: %w", ResticCmdTimeoutError)
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
err := cmd.Wait()
|
err := cmd.Wait()
|
||||||
|
|
|
@ -189,7 +189,7 @@ func TestBackup_TakesLongerThanNeededToOutput_TimesOut(t *testing.T) {
|
||||||
wrapper := Wrapper{}
|
wrapper := Wrapper{}
|
||||||
err := wrapper.Backup(&Config{})
|
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) {
|
func TestUpdateLatestSnapshot_TakesLongerThanNeededToOutput_TimesOut(t *testing.T) {
|
||||||
|
@ -200,7 +200,7 @@ func TestUpdateLatestSnapshot_TakesLongerThanNeededToOutput_TimesOut(t *testing.
|
||||||
err := wrapper.UpdateLatestSnapshots(&Config{})
|
err := wrapper.UpdateLatestSnapshots(&Config{})
|
||||||
|
|
||||||
assert.Empty(t, wrapper.LatestSnapshots)
|
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) {
|
func TestUpdateLatestSnapshots_FromJSONOutputOfRestic(t *testing.T) {
|
||||||
|
|
Loading…
Reference in New Issue