gitee.com/ks-custle/core-gm@v0.0.0-20230922171213-b83bdd97b62c/grpc/internal/status/status.go (about)

     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  	"gitee.com/ks-custle/core-gm/grpc/codes"
    34  	"github.com/golang/protobuf/proto"
    35  	spb "google.golang.org/genproto/googleapis/rpc/status"
    36  	"google.golang.org/protobuf/types/known/anypb"
    37  )
    38  
    39  // Status represents an RPC status code, message, and details.  It is immutable
    40  // and should be created with New, Newf, or FromProto.
    41  type Status struct {
    42  	s *spb.Status
    43  }
    44  
    45  // New returns a Status representing c and msg.
    46  func New(c codes.Code, msg string) *Status {
    47  	return &Status{s: &spb.Status{Code: int32(c), Message: msg}}
    48  }
    49  
    50  // Newf returns New(c, fmt.Sprintf(format, a...)).
    51  //
    52  //goland:noinspection GoUnusedExportedFunction
    53  func Newf(c codes.Code, format string, a ...interface{}) *Status {
    54  	return New(c, fmt.Sprintf(format, a...))
    55  }
    56  
    57  // FromProto returns a Status representing s.
    58  func FromProto(s *spb.Status) *Status {
    59  	return &Status{s: proto.Clone(s).(*spb.Status)}
    60  }
    61  
    62  // Err returns an error representing c and msg.  If c is OK, returns nil.
    63  func Err(c codes.Code, msg string) error {
    64  	return New(c, msg).Err()
    65  }
    66  
    67  // Errorf returns Error(c, fmt.Sprintf(format, a...)).
    68  //
    69  //goland:noinspection GoUnusedExportedFunction
    70  func Errorf(c codes.Code, format string, a ...interface{}) error {
    71  	return Err(c, fmt.Sprintf(format, a...))
    72  }
    73  
    74  // Code returns the status code contained in s.
    75  func (s *Status) Code() codes.Code {
    76  	if s == nil || s.s == nil {
    77  		return codes.OK
    78  	}
    79  	return codes.Code(s.s.Code)
    80  }
    81  
    82  // Message returns the message contained in s.
    83  func (s *Status) Message() string {
    84  	if s == nil || s.s == nil {
    85  		return ""
    86  	}
    87  	return s.s.Message
    88  }
    89  
    90  // Proto returns s's status as an spb.Status proto message.
    91  func (s *Status) Proto() *spb.Status {
    92  	if s == nil {
    93  		return nil
    94  	}
    95  	return proto.Clone(s.s).(*spb.Status)
    96  }
    97  
    98  // Err returns an immutable error representing s; returns nil if s.Code() is OK.
    99  func (s *Status) Err() error {
   100  	if s.Code() == codes.OK {
   101  		return nil
   102  	}
   103  	return &Error{s: s}
   104  }
   105  
   106  // WithDetails returns a new status with the provided details messages appended to the status.
   107  // If any errors are encountered, it returns nil and the first error encountered.
   108  func (s *Status) WithDetails(details ...proto.Message) (*Status, error) {
   109  	if s.Code() == codes.OK {
   110  		return nil, errors.New("no error details for status with code OK")
   111  	}
   112  	// s.Code() != OK implies that s.Proto() != nil.
   113  	p := s.Proto()
   114  	for _, detail := range details {
   115  		// ptypes.MarshalAny is deprecated, Call the anypb.New function instead.
   116  		//any, err := ptypes.MarshalAny(detail)
   117  		any, err := anypb.New(proto.MessageV2(detail))
   118  		if err != nil {
   119  			return nil, err
   120  		}
   121  		p.Details = append(p.Details, any)
   122  	}
   123  	return &Status{s: p}, nil
   124  }
   125  
   126  // Details returns a slice of details messages attached to the status.
   127  // If a detail cannot be decoded, the error is returned in place of the detail.
   128  func (s *Status) Details() []interface{} {
   129  	if s == nil || s.s == nil {
   130  		return nil
   131  	}
   132  	details := make([]interface{}, 0, len(s.s.Details))
   133  	for _, any := range s.s.Details {
   134  		// ptypes.DynamicAny 与 ptypes.UnmarshalAny 已弃用,改为直接使用 any.UnmarshalNew()
   135  		//detail := &ptypes.DynamicAny{}
   136  		//if err := ptypes.UnmarshalAny(any, detail); err != nil {
   137  		//	details = append(details, err)
   138  		//	continue
   139  		//}
   140  		//details = append(details, detail.Message)
   141  		detail, err := any.UnmarshalNew()
   142  		if err != nil {
   143  			details = append(details, err)
   144  			continue
   145  		}
   146  		details = append(details, detail)
   147  	}
   148  	return details
   149  }
   150  
   151  func (s *Status) String() string {
   152  	return fmt.Sprintf("rpc error: code = %s desc = %s", s.Code(), s.Message())
   153  }
   154  
   155  // Error wraps a pointer of a status proto. It implements error and Status,
   156  // and a nil *Error should never be returned by this package.
   157  type Error struct {
   158  	s *Status
   159  }
   160  
   161  func (e *Error) Error() string {
   162  	return e.s.String()
   163  }
   164  
   165  // GRPCStatus returns the Status represented by se.
   166  func (e *Error) GRPCStatus() *Status {
   167  	return e.s
   168  }
   169  
   170  // Is implements future error.Is functionality.
   171  // A Error is equivalent if the code and message are identical.
   172  func (e *Error) Is(target error) bool {
   173  	tse, ok := target.(*Error)
   174  	if !ok {
   175  		return false
   176  	}
   177  	return proto.Equal(e.s.s, tse.s.s)
   178  }