Skip to content

Commit

Permalink
*: handle more nil error cases (pingcap#129)
Browse files Browse the repository at this point in the history
  • Loading branch information
xhebox committed Mar 13, 2023
1 parent e25f2da commit 8b373b8
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 11 deletions.
3 changes: 3 additions & 0 deletions lib/util/errors/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ type Error struct {

// WithStack will wrapping an error with stacktrace, given a default stack depth.
func WithStack(err error) error {
if err == nil {
return nil
}
e := &Error{err: err}
e.withStackDepth(1, defaultStackDepth)
return e
Expand Down
2 changes: 2 additions & 0 deletions lib/util/errors/error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ func TestStacktrace(t *testing.T) {
require.Contains(t, fmt.Sprintf("%+v", e), t.Name(), "stacktrace must contain test name")
require.Contains(t, fmt.Sprintf("%v", e), t.Name(), "stacktrace must contain test name")
require.Contains(t, fmt.Sprintf("%+s", e), t.Name(), "stacktrace must contain test name")

require.Nil(t, serr.WithStack(nil), "wrap nil got nil")
}

func TestUnwrap(t *testing.T) {
Expand Down
3 changes: 3 additions & 0 deletions lib/util/errors/werror.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ func (e *WError) Unwrap() error {
// Note that wrap nil error will get nil error.
func Wrap(cerr error, uerr error) error {
if cerr == nil {
return uerr
}
if uerr == nil {
return nil
}
return &WError{
Expand Down
3 changes: 2 additions & 1 deletion lib/util/errors/werror_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ func TestWrap(t *testing.T) {
require.ErrorIsf(t, e, e1, "equal to the external error")
require.ErrorAsf(t, e, &e2, "unwrapping to the internal error")

require.Nil(t, serr.Wrap(nil, e2), "wrap nil got nil")
require.Equal(t, e2, serr.Wrap(nil, e2), "wrap with nil got the original")
require.Nil(t, serr.Wrap(e2, nil), "wrap nil got nil")
}

func TestWrapf(t *testing.T) {
Expand Down
12 changes: 2 additions & 10 deletions pkg/proxy/net/packetio.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,7 @@ func NewPacketIO(conn net.Conn, opts ...PacketIOption) *PacketIO {
}

func (p *PacketIO) wrapErr(err error) error {
e := err
if p.wrap != nil {
e = errors.Wrap(p.wrap, err)
}
e = errors.WithStack(e)
return e
return errors.WithStack(errors.Wrap(p.wrap, err))
}

// Proxy returned parsed proxy header from clients if any.
Expand Down Expand Up @@ -248,9 +243,6 @@ func (p *PacketIO) Flush() error {

func (p *PacketIO) Close() error {
var errs []error
if p.wrap != nil {
errs = append(errs, p.wrap)
}
/*
TODO: flush when we want to smoothly exit
if err := p.Flush(); err != nil {
Expand All @@ -262,5 +254,5 @@ func (p *PacketIO) Close() error {
errs = append(errs, err)
}
}
return errors.Collect(ErrCloseConn, errs...)
return p.wrapErr(errors.Collect(ErrCloseConn, errs...))
}

0 comments on commit 8b373b8

Please sign in to comment.