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 (
|
||||
"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 {
|
||||
|
|
|
@ -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()
|
||||
|
|
|
@ -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) {
|
||||
|
|
Loading…
Reference in New Issue