Skip to content

Commit

Permalink
Use Go 1.13 error wrapping
Browse files Browse the repository at this point in the history
Migrate from github.com/pkg/errors to fmt.Errorf wrapping with %w.
  • Loading branch information
lmb committed Apr 28, 2020
1 parent 1165df3 commit da237db
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 34 deletions.
8 changes: 3 additions & 5 deletions child.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import (
"encoding/gob"
"fmt"
"os"

"github.com/pkg/errors"
)

type child struct {
Expand All @@ -22,14 +20,14 @@ func startChild(env *env, passedFiles map[fileName]*file) (*child, error) {
// readyW is passed to the child, readyR stays with the parent
readyR, readyW, err := os.Pipe()
if err != nil {
return nil, errors.Wrap(err, "pipe failed")
return nil, fmt.Errorf("pipe failed: %s", err)
}

namesR, namesW, err := os.Pipe()
if err != nil {
readyR.Close()
readyW.Close()
return nil, errors.Wrap(err, "pipe failed")
return nil, fmt.Errorf("pipe failed: %s", err)
}

// Copy passed fds and append the notification pipe
Expand All @@ -53,7 +51,7 @@ func startChild(env *env, passedFiles map[fileName]*file) (*child, error) {
readyW.Close()
namesR.Close()
namesW.Close()
return nil, errors.Wrapf(err, "can't start process %s", os.Args[0])
return nil, fmt.Errorf("can't start process %s: %s", os.Args[0], err)
}

exited := make(chan struct{})
Expand Down
5 changes: 2 additions & 3 deletions dup_fd.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@
package tableflip

import (
"fmt"
"syscall"

"github.com/pkg/errors"
)

func dupFd(fd uintptr, name fileName) (*file, error) {
dupfd, _, errno := syscall.Syscall(syscall.SYS_FCNTL, fd, syscall.F_DUPFD_CLOEXEC, 0)
if errno != 0 {
return nil, errors.Wrap(errno, "can't dup fd using fcntl")
return nil, fmt.Errorf("can't dup fd using fcntl: %s", errno)
}

return newFile(dupfd, name), nil
Expand Down
15 changes: 7 additions & 8 deletions fds.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package tableflip

import (
"fmt"
"net"
"os"
"runtime"
"strings"
"sync"
"syscall"

"github.com/pkg/errors"
)

// Listener can be shared between processes.
Expand Down Expand Up @@ -94,12 +93,12 @@ func (f *Fds) Listen(network, addr string) (net.Listener, error) {

ln, err = net.Listen(network, addr)
if err != nil {
return nil, errors.Wrap(err, "can't create new listener")
return nil, fmt.Errorf("can't create new listener: %s", err)
}

if _, ok := ln.(Listener); !ok {
ln.Close()
return nil, errors.Errorf("%T doesn't implement tableflip.Listener", ln)
return nil, fmt.Errorf("%T doesn't implement tableflip.Listener", ln)
}

err = f.addListenerLocked(network, addr, ln.(Listener))
Expand Down Expand Up @@ -130,7 +129,7 @@ func (f *Fds) listenerLocked(network, addr string) (net.Listener, error) {

ln, err := net.FileListener(file.File)
if err != nil {
return nil, errors.Wrapf(err, "can't inherit listener %s %s", network, addr)
return nil, fmt.Errorf("can't inherit listener %s %s: %s", network, addr, err)
}

delete(f.inherited, key)
Expand Down Expand Up @@ -176,7 +175,7 @@ func (f *Fds) Conn(network, addr string) (net.Conn, error) {

conn, err := net.FileConn(file.File)
if err != nil {
return nil, errors.Wrapf(err, "can't inherit connection %s %s", network, addr)
return nil, fmt.Errorf("can't inherit connection %s %s: %s", network, addr, err)
}

delete(f.inherited, key)
Expand All @@ -198,7 +197,7 @@ func (f *Fds) addConnLocked(kind, network, addr string, conn syscall.Conn) error
key := fileName{kind, network, addr}
file, err := dupConn(conn, key)
if err != nil {
return errors.Wrapf(err, "can't dup %s (%s %s)", kind, network, addr)
return fmt.Errorf("can't dup %s (%s %s): %s", kind, network, addr, err)
}

delete(f.inherited, key)
Expand Down Expand Up @@ -336,7 +335,7 @@ func dupConn(conn syscall.Conn, name fileName) (*file, error) {
dup, duperr = dupFd(fd, name)
})
if err != nil {
return nil, errors.Wrap(err, "can't access fd")
return nil, fmt.Errorf("can't access fd: %s", err)
}
return dup, duperr
}
2 changes: 0 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
module github.com/cloudflare/tableflip

go 1.13

require github.com/pkg/errors v0.9.1
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +0,0 @@
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
10 changes: 5 additions & 5 deletions parent.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ package tableflip

import (
"encoding/gob"
"errors"
"fmt"
"io"
"io/ioutil"
"os"

"github.com/pkg/errors"
)

const (
Expand All @@ -31,7 +31,7 @@ func newParent(env *env) (*parent, map[fileName]*file, error) {
var names [][]string
dec := gob.NewDecoder(rd)
if err := dec.Decode(&names); err != nil {
return nil, nil, errors.Wrap(err, "can't decode names from parent process")
return nil, nil, fmt.Errorf("can't decode names from parent process: %s", err)
}

files := make(map[fileName]*file)
Expand All @@ -58,7 +58,7 @@ func newParent(env *env) (*parent, map[fileName]*file, error) {
if n != 0 {
err = errors.New("unexpected data from parent process")
} else if err != nil {
err = errors.Wrap(err, "unexpected error while waiting for parent to exit")
err = fmt.Errorf("unexpected error while waiting for parent to exit: %s", err)
}
result <- err
close(exited)
Expand All @@ -74,7 +74,7 @@ func newParent(env *env) (*parent, map[fileName]*file, error) {
func (ps *parent) sendReady() error {
defer ps.wr.Close()
if _, err := ps.wr.Write([]byte{notifyReady}); err != nil {
return errors.Wrap(err, "can't notify parent process")
return fmt.Errorf("can't notify parent process: %s", err)
}
return nil
}
18 changes: 9 additions & 9 deletions upgrader.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ package tableflip

import (
"context"
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"runtime"
"strconv"
"sync"
"time"

"github.com/pkg/errors"
)

// DefaultUpgradeTimeout is the duration before the Upgrader kills the new process if no
Expand Down Expand Up @@ -52,13 +52,13 @@ var ErrNotSupported = errors.New("tableflip: platform does not support graceful

// New creates a new Upgrader. Files are passed from the parent and may be empty.
//
// Only the first call to this function will succeed.
// Only the first call to this function will succeed. May return ErrNotSupported.
func New(opts Options) (upg *Upgrader, err error) {
stdEnvMu.Lock()
defer stdEnvMu.Unlock()

if !isSupportedOS() {
return nil, ErrNotSupported
return nil, fmt.Errorf("%w", ErrNotSupported)
}

if stdEnvUpgrader != nil {
Expand Down Expand Up @@ -116,7 +116,7 @@ func (u *Upgrader) Ready() error {

if u.opts.PIDFile != "" {
if err := writePIDFile(u.opts.PIDFile); err != nil {
return errors.Wrap(err, "tableflip: can't write PID file")
return fmt.Errorf("tableflip: can't write PID file: %s", err)
}
}

Expand Down Expand Up @@ -244,7 +244,7 @@ func (u *Upgrader) run() {
func (u *Upgrader) doUpgrade() (*os.File, error) {
child, err := startChild(u.env, u.Fds.copy())
if err != nil {
return nil, errors.Wrap(err, "can't start child")
return nil, fmt.Errorf("can't start child: %s", err)
}

readyTimeout := time.After(u.opts.UpgradeTimeout)
Expand All @@ -255,17 +255,17 @@ func (u *Upgrader) doUpgrade() (*os.File, error) {

case err := <-child.result:
if err == nil {
return nil, errors.Errorf("child %s exited", child)
return nil, fmt.Errorf("child %s exited", child)
}
return nil, errors.Wrapf(err, "child %s exited", child)
return nil, fmt.Errorf("child %s exited: %s", child, err)

case <-u.stopC:
child.Kill()
return nil, errors.New("terminating")

case <-readyTimeout:
child.Kill()
return nil, errors.Errorf("new child %s timed out", child)
return nil, fmt.Errorf("new child %s timed out", child)

case file := <-child.ready:
return file, nil
Expand Down

0 comments on commit da237db

Please sign in to comment.