errcheck: More error handling

This commit is contained in:
Alexander Neumann 2021-01-30 16:46:34 +01:00
parent 16313bfcc9
commit 3c753c071c
5 changed files with 32 additions and 12 deletions

View file

@ -63,9 +63,9 @@ func run(command string, args ...string) (*StdioConn, *exec.Cmd, *sync.WaitGroup
stdout, w, err := os.Pipe() stdout, w, err := os.Pipe()
if err != nil { if err != nil {
// close first pipe // close first pipe and ignore subsequent errors
r.Close() _ = r.Close()
stdin.Close() _ = stdin.Close()
return nil, nil, nil, nil, err return nil, nil, nil, nil, err
} }
@ -197,8 +197,8 @@ func newBackend(cfg Config, lim limiter.Limiter) (*Backend, error) {
err := cmd.Wait() err := cmd.Wait()
debug.Log("Wait returned %v", err) debug.Log("Wait returned %v", err)
be.waitResult = err be.waitResult = err
// close our side of the pipes to rclone // close our side of the pipes to rclone, ignore errors
stdioConn.CloseAll() _ = stdioConn.CloseAll()
close(waitCh) close(waitCh)
}() }()
@ -237,13 +237,17 @@ func newBackend(cfg Config, lim limiter.Limiter) (*Backend, error) {
res, err := ctxhttp.Do(ctx, client, req) res, err := ctxhttp.Do(ctx, client, req)
if err != nil { if err != nil {
bg() // ignore subsequent errors
_ = bg()
_ = cmd.Process.Kill() _ = cmd.Process.Kill()
return nil, errors.Errorf("error talking HTTP to rclone: %v", err) return nil, errors.Errorf("error talking HTTP to rclone: %v", err)
} }
debug.Log("HTTP status %q returned, moving instance to background", res.Status) debug.Log("HTTP status %q returned, moving instance to background", res.Status)
bg() err = bg()
if err != nil {
return nil, fmt.Errorf("error moving process to background: %w", err)
}
return be, nil return be, nil
} }

View file

@ -23,7 +23,9 @@ func TestRcloneExit(t *testing.T) {
return return
} }
rtest.OK(t, err) rtest.OK(t, err)
defer be.Close() defer func() {
rtest.OK(t, be.Close())
}()
err = be.cmd.Process.Kill() err = be.cmd.Process.Kill()
rtest.OK(t, err) rtest.OK(t, err)

View file

@ -327,7 +327,12 @@ func (c *Checker) Structure(ctx context.Context, p *progress.Counter, errChan ch
}) })
} }
wg.Wait() // the wait group should not return an error because no worker returns an
// error, so panic if that has changed somehow.
err := wg.Wait()
if err != nil {
panic(err)
}
} }
func (c *Checker) checkTree(id restic.ID, tree *restic.Tree) (errs []error) { func (c *Checker) checkTree(id restic.ID, tree *restic.Tree) (errs []error) {

View file

@ -79,7 +79,10 @@ func NewBackup(term *termstatus.Terminal, verbosity uint) *Backup {
func toJSONString(status interface{}) string { func toJSONString(status interface{}) string {
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
json.NewEncoder(buf).Encode(status) err := json.NewEncoder(buf).Encode(status)
if err != nil {
panic(err)
}
return buf.String() return buf.String()
} }

View file

@ -28,17 +28,23 @@ func buildTreeMap(tree TestTree, m TreeMap) restic.ID {
for name, item := range tree { for name, item := range tree {
switch elem := item.(type) { switch elem := item.(type) {
case TestFile: case TestFile:
res.Insert(&restic.Node{ err := res.Insert(&restic.Node{
Name: name, Name: name,
Type: "file", Type: "file",
}) })
if err != nil {
panic(err)
}
case TestTree: case TestTree:
id := buildTreeMap(elem, m) id := buildTreeMap(elem, m)
res.Insert(&restic.Node{ err := res.Insert(&restic.Node{
Name: name, Name: name,
Subtree: &id, Subtree: &id,
Type: "dir", Type: "dir",
}) })
if err != nil {
panic(err)
}
default: default:
panic(fmt.Sprintf("invalid type %T", elem)) panic(fmt.Sprintf("invalid type %T", elem))
} }