errors: add missing canceled

example: add auth
metadata interceptor: do not overide metadatas

Signed-off-by: Adphi <philippe.adrien.nousse@gmail.com>
This commit is contained in:
2022-06-24 11:47:09 +02:00
parent 79771e58c1
commit 3fb566cb80
3 changed files with 63 additions and 6 deletions

View File

@ -1,6 +1,10 @@
package errors
import (
"context"
"errors"
"strings"
status2 "google.golang.org/genproto/googleapis/rpc/status"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
@ -8,6 +12,10 @@ import (
"google.golang.org/protobuf/types/known/anypb"
)
func Canceled(err error) error {
return status.Error(codes.Canceled, err.Error())
}
func InvalidArgument(err error) error {
return status.Error(codes.InvalidArgument, err.Error())
}
@ -63,6 +71,12 @@ func makeDetails(m ...proto.Message) []*anypb.Any {
return out
}
func Canceledf(msg string, args ...interface{}) error {
return status.Errorf(codes.Canceled, msg, args...)
}
func Canceledd(err error, details ...proto.Message) error {
return statusErr(codes.Canceled, err, details...)
}
func InvalidArgumentf(msg string, args ...interface{}) error {
return status.Errorf(codes.InvalidArgument, msg, args...)
}
@ -152,7 +166,7 @@ func IsCanceled(err error) bool {
if err == nil {
return false
}
return status.Convert(err).Code() == codes.Canceled
return status.Convert(err).Code() == codes.Canceled || IsCanceled(err)
}
func IsUnknown(err error) bool {
if err == nil {
@ -170,7 +184,7 @@ func IsDeadlineExceeded(err error) bool {
if err == nil {
return false
}
return status.Convert(err).Code() == codes.DeadlineExceeded
return status.Convert(err).Code() == codes.DeadlineExceeded || IsContextDeadlineExceeded(err)
}
func IsNotFound(err error) bool {
if err == nil {
@ -244,3 +258,30 @@ func IsUnauthenticated(err error) bool {
}
return status.Convert(err).Code() == codes.Unauthenticated
}
func IsContextCanceled(err error) bool {
err = Unwrap(err)
if err == nil {
return false
}
return strings.Contains(err.Error(), context.Canceled.Error())
}
func IsContextDeadlineExceeded(err error) bool {
err = Unwrap(err)
if err == nil {
return false
}
return strings.Contains(err.Error(), context.DeadlineExceeded.Error())
}
func Unwrap(err error) error {
s, ok := status.FromError(err)
if s == nil {
return nil
}
if ok {
return errors.New(s.Message())
}
return err
}