Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

grpcerrors: choose the error code from the error chain in a depth-first order #5219

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 81 additions & 0 deletions util/errutil/errutil.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package errutil

type Iterator struct {
// unvisited is a stack structure where the last element
// is the top of the stack.
unvisited []error
}

// Iterate will iterate through the error chain in a depth-first order.
func Iterate(err error) *Iterator {
i := &Iterator{}
if err != nil {
i.unvisited = []error{err}
}
return i
}

// IsValid returns if this iterator is valid.
func (i *Iterator) IsValid() bool {
return len(i.unvisited) > 0
}

// Next will retrieve the next error in the chain.
func (i *Iterator) Next() {
if len(i.unvisited) == 0 {
return
}

err := i.pop()
switch err := err.(type) {
case interface{ Unwrap() error }:
if child := err.Unwrap(); child != nil {
i.push(child)
}
case interface{ Unwrap() []error }:
children := err.Unwrap()
if len(children) == 0 {
// Fast path although unlikely.
return
}
i.push(children...)
}
}

// Err will return the current error for the Iterator.
// It is only valid to invoke this method if IsValid returns true.
func (i *Iterator) Err() error {
last := len(i.unvisited) - 1
return i.unvisited[last]
}

func (i *Iterator) pop() (err error) {
err, i.unvisited = pop(i.unvisited)
return err
}

func (i *Iterator) push(elems ...error) {
i.unvisited = push(i.unvisited, elems...)
}

func pop[T any](s []T) (elem T, rest []T) {
elem = s[len(s)-1]
rest = s[:len(s)-1]
return elem, rest
}

func push[T any](s []T, elems ...T) []T {
if len(elems) == 1 {
return append(s, elems...)
}

// If there are multiple elements, batch
// add them and reverse the order so the first
// element is the last in the array (FIFO).
i := len(s)
s = append(s, elems...)
for j := len(s) - 1; i < j; i, j = i+1, j-1 {
s[i], s[j] = s[j], s[i]
}
return s
}
41 changes: 18 additions & 23 deletions util/grpcerrors/grpcerrors.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (
gogotypes "github.com/gogo/protobuf/types"
"github.com/golang/protobuf/proto" //nolint:staticcheck
"github.com/golang/protobuf/ptypes/any"
"github.com/moby/buildkit/errdefs"
"github.com/moby/buildkit/util/bklog"
"github.com/moby/buildkit/util/errutil"
"github.com/moby/buildkit/util/stack"
spb "google.golang.org/genproto/googleapis/rpc/status"
"google.golang.org/grpc/codes"
Expand Down Expand Up @@ -95,31 +95,26 @@ func withDetails(ctx context.Context, s *status.Status, details ...proto.Message
}

func Code(err error) codes.Code {
if errdefs.IsInternal(err) {
return codes.Internal
}

if se, ok := err.(interface {
Code() codes.Code
}); ok {
return se.Code()
}

if se, ok := err.(interface {
GRPCStatus() *status.Status
}); ok {
return se.GRPCStatus().Code()
}
for i := errutil.Iterate(err); i.IsValid(); i.Next() {
switch err := i.Err().(type) {
case interface{ System() }:
return codes.Internal
case interface{ GRPCStatus() *status.Status }:
if st := err.GRPCStatus(); st != nil {
return st.Code()
}
case interface{ Code() codes.Code }:
return err.Code()
}

wrapped, ok := err.(interface {
Unwrap() error
})
if ok {
if err := wrapped.Unwrap(); err != nil {
return Code(err)
switch i.Err() {
case context.Canceled:
return codes.Canceled
case context.DeadlineExceeded:
return codes.DeadlineExceeded
}
}
return status.FromContextError(err).Code()
return codes.Unknown
}

func WrapCode(err error, code codes.Code) error {
Expand Down
Loading