Skip to content

Commit

Permalink
*: filter nil errors (#97)
Browse files Browse the repository at this point in the history
  • Loading branch information
xhebox authored Sep 24, 2022
1 parent 0354249 commit fefaaa3
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 26 deletions.
10 changes: 9 additions & 1 deletion lib/util/errors/merror.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func (e *MError) Error() string {
func (e *MError) Is(s error) bool {
is := errors.Is(e.cerr, s)
for _, e := range e.uerr {
is = is || errors.Is(e, s)
is = is || errors.Is(e, s)
if is {
break
}
Expand All @@ -78,6 +78,14 @@ func (e *MError) Cause() []error {

// Collect is used to collect multiple errors. `Unwrap` is noop and `Is(err, ErrMine) == true`. While `As(err, underlyingError)` do not work, you can still get underlying errors by `MError.Cause`.
func Collect(cerr error, uerr ...error) error {
n := 0
for _, e := range uerr {
if e != nil {
uerr[n] = e
n++
}
}
uerr = uerr[:n]
if len(uerr) == 0 {
return nil
}
Expand Down
4 changes: 4 additions & 0 deletions lib/util/errors/merror_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,8 @@ func TestCollect(t *testing.T) {
require.ErrorIsf(t, e, e1, "but errors.Is works for all errors")
require.Equal(t, e.(*serr.MError).Cause(), []error{e2, e3}, "get underlying errors")
require.NoError(t, serr.Collect(e3), "nil if there is no underlying error")

e4 := serr.Collect(e1, e2, nil).(*serr.MError)
require.Len(t, e4.Cause(), 1, "collect non-nil erros only")
require.NoError(t, serr.Collect(e3, nil, nil), "nil if all errors are nil")
}
9 changes: 1 addition & 8 deletions pkg/proxy/client/client_conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,5 @@ func (cc *ClientConnection) processMsg(ctx context.Context) error {
}

func (cc *ClientConnection) Close() error {
var errs []error
if err := cc.pkt.Close(); err != nil {
errs = append(errs, err)
}
if err := cc.connMgr.Close(); err != nil {
errs = append(errs, err)
}
return errors.Collect(ErrCloseConn, errs...)
return errors.Collect(ErrCloseConn, cc.pkt.Close(), cc.connMgr.Close())
}
6 changes: 2 additions & 4 deletions pkg/proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,11 +158,9 @@ func (s *SQLServer) onConn(ctx context.Context, conn net.Conn) {

// Close closes the server.
func (s *SQLServer) Close() error {
var errs []error
errs := make([]error, 0, 4)
if s.listener != nil {
if err := s.listener.Close(); err != nil {
errs = append(errs, err)
}
errs = append(errs, s.listener.Close())
}

s.mu.Lock()
Expand Down
18 changes: 5 additions & 13 deletions pkg/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,26 +205,18 @@ func (s *Server) Run(ctx context.Context) {
}

func (s *Server) Close() error {
var errs []error
errs := make([]error, 0, 4)
if s.Proxy != nil {
if err := s.Proxy.Close(); err != nil {
errs = append(errs, err)
}
errs = append(errs, s.Proxy.Close())
}
if s.NamespaceManager != nil {
if err := s.NamespaceManager.Close(); err != nil {
errs = append(errs, err)
}
errs = append(errs, s.NamespaceManager.Close())
}
if s.ConfigManager != nil {
if err := s.ConfigManager.Close(); err != nil {
errs = append(errs, err)
}
errs = append(errs, s.ConfigManager.Close())
}
if s.ObserverClient != nil {
if err := s.ObserverClient.Close(); err != nil {
errs = append(errs, err)
}
errs = append(errs, s.ObserverClient.Close())
}
if s.Etcd != nil {
var wg waitgroup.WaitGroup
Expand Down

0 comments on commit fefaaa3

Please sign in to comment.