...

Source file src/google.golang.org/grpc/internal/status/status.go

Documentation: google.golang.org/grpc/internal/status

     1  /*
     2   *
     3   * Copyright 2020 gRPC authors.
     4   *
     5   * Licensed under the Apache License, Version 2.0 (the "License");
     6   * you may not use this file except in compliance with the License.
     7   * You may obtain a copy of the License at
     8   *
     9   *     http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   *
    17   */
    18  
    19  // Package status implements errors returned by gRPC.  These errors are
    20  // serialized and transmitted on the wire between server and client, and allow
    21  // for additional data to be transmitted via the Details field in the status
    22  // proto.  gRPC service handlers should return an error created by this
    23  // package, and gRPC clients should expect a corresponding error to be
    24  // returned from the RPC call.
    25  //
    26  // This package upholds the invariants that a non-nil error may not
    27  // contain an OK code, and an OK code must result in a nil error.
    28  package status
    29  
    30  import (
    31  	"errors"
    32  	"fmt"
    33  
    34  	spb "google.golang.org/genproto/googleapis/rpc/status"
    35  	"google.golang.org/grpc/codes"
    36  	"google.golang.org/protobuf/proto"
    37  	"google.golang.org/protobuf/protoadapt"
    38  	"google.golang.org/protobuf/types/known/anypb"
    39  )
    40  
    41  // Status represents an RPC status code, message, and details.  It is immutable
    42  // and should be created with New, Newf, or FromProto.
    43  type Status struct {
    44  	s *spb.Status
    45  }
    46  
    47  // NewWithProto returns a new status including details from statusProto.  This
    48  // is meant to be used by the gRPC library only.
    49  func NewWithProto(code codes.Code, message string, statusProto []string) *Status {
    50  	if len(statusProto) != 1 {
    51  		// No grpc-status-details bin header, or multiple; just ignore.
    52  		return &Status{s: &spb.Status{Code: int32(code), Message: message}}
    53  	}
    54  	st := &spb.Status{}
    55  	if err := proto.Unmarshal([]byte(statusProto[0]), st); err != nil {
    56  		// Probably not a google.rpc.Status proto; do not provide details.
    57  		return &Status{s: &spb.Status{Code: int32(code), Message: message}}
    58  	}
    59  	if st.Code == int32(code) {
    60  		// The codes match between the grpc-status header and the
    61  		// grpc-status-details-bin header; use the full details proto.
    62  		return &Status{s: st}
    63  	}
    64  	return &Status{
    65  		s: &spb.Status{
    66  			Code: int32(codes.Internal),
    67  			Message: fmt.Sprintf(
    68  				"grpc-status-details-bin mismatch: grpc-status=%v, grpc-message=%q, grpc-status-details-bin=%+v",
    69  				code, message, st,
    70  			),
    71  		},
    72  	}
    73  }
    74  
    75  // New returns a Status representing c and msg.
    76  func New(c codes.Code, msg string) *Status {
    77  	return &Status{s: &spb.Status{Code: int32(c), Message: msg}}
    78  }
    79  
    80  // Newf returns New(c, fmt.Sprintf(format, a...)).
    81  func Newf(c codes.Code, format string, a ...any) *Status {
    82  	return New(c, fmt.Sprintf(format, a...))
    83  }
    84  
    85  // FromProto returns a Status representing s.
    86  func FromProto(s *spb.Status) *Status {
    87  	return &Status{s: proto.Clone(s).(*spb.Status)}
    88  }
    89  
    90  // Err returns an error representing c and msg.  If c is OK, returns nil.
    91  func Err(c codes.Code, msg string) error {
    92  	return New(c, msg).Err()
    93  }
    94  
    95  // Errorf returns Error(c, fmt.Sprintf(format, a...)).
    96  func Errorf(c codes.Code, format string, a ...any) error {
    97  	return Err(c, fmt.Sprintf(format, a...))
    98  }
    99  
   100  // Code returns the status code contained in s.
   101  func (s *Status) Code() codes.Code {
   102  	if s == nil || s.s == nil {
   103  		return codes.OK
   104  	}
   105  	return codes.Code(s.s.Code)
   106  }
   107  
   108  // Message returns the message contained in s.
   109  func (s *Status) Message() string {
   110  	if s == nil || s.s == nil {
   111  		return ""
   112  	}
   113  	return s.s.Message
   114  }
   115  
   116  // Proto returns s's status as an spb.Status proto message.
   117  func (s *Status) Proto() *spb.Status {
   118  	if s == nil {
   119  		return nil
   120  	}
   121  	return proto.Clone(s.s).(*spb.Status)
   122  }
   123  
   124  // Err returns an immutable error representing s; returns nil if s.Code() is OK.
   125  func (s *Status) Err() error {
   126  	if s.Code() == codes.OK {
   127  		return nil
   128  	}
   129  	return &Error{s: s}
   130  }
   131  
   132  // WithDetails returns a new status with the provided details messages appended to the status.
   133  // If any errors are encountered, it returns nil and the first error encountered.
   134  func (s *Status) WithDetails(details ...protoadapt.MessageV1) (*Status, error) {
   135  	if s.Code() == codes.OK {
   136  		return nil, errors.New("no error details for status with code OK")
   137  	}
   138  	// s.Code() != OK implies that s.Proto() != nil.
   139  	p := s.Proto()
   140  	for _, detail := range details {
   141  		any, err := anypb.New(protoadapt.MessageV2Of(detail))
   142  		if err != nil {
   143  			return nil, err
   144  		}
   145  		p.Details = append(p.Details, any)
   146  	}
   147  	return &Status{s: p}, nil
   148  }
   149  
   150  // Details returns a slice of details messages attached to the status.
   151  // If a detail cannot be decoded, the error is returned in place of the detail.
   152  func (s *Status) Details() []any {
   153  	if s == nil || s.s == nil {
   154  		return nil
   155  	}
   156  	details := make([]any, 0, len(s.s.Details))
   157  	for _, any := range s.s.Details {
   158  		detail, err := any.UnmarshalNew()
   159  		if err != nil {
   160  			details = append(details, err)
   161  			continue
   162  		}
   163  		details = append(details, detail)
   164  	}
   165  	return details
   166  }
   167  
   168  func (s *Status) String() string {
   169  	return fmt.Sprintf("rpc error: code = %s desc = %s", s.Code(), s.Message())
   170  }
   171  
   172  // Error wraps a pointer of a status proto. It implements error and Status,
   173  // and a nil *Error should never be returned by this package.
   174  type Error struct {
   175  	s *Status
   176  }
   177  
   178  func (e *Error) Error() string {
   179  	return e.s.String()
   180  }
   181  
   182  // GRPCStatus returns the Status represented by se.
   183  func (e *Error) GRPCStatus() *Status {
   184  	return e.s
   185  }
   186  
   187  // Is implements future error.Is functionality.
   188  // A Error is equivalent if the code and message are identical.
   189  func (e *Error) Is(target error) bool {
   190  	tse, ok := target.(*Error)
   191  	if !ok {
   192  		return false
   193  	}
   194  	return proto.Equal(e.s.s, tse.s.s)
   195  }
   196  
   197  // IsRestrictedControlPlaneCode returns whether the status includes a code
   198  // restricted for control plane usage as defined by gRFC A54.
   199  func IsRestrictedControlPlaneCode(s *Status) bool {
   200  	switch s.Code() {
   201  	case codes.InvalidArgument, codes.NotFound, codes.AlreadyExists, codes.FailedPrecondition, codes.Aborted, codes.OutOfRange, codes.DataLoss:
   202  		return true
   203  	}
   204  	return false
   205  }
   206  

View as plain text