github.com/RomiChan/protobuf@v0.1.1-0.20230204044148-2ed269a2e54d/proto/error.go (about)

     1  package proto
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"reflect"
     7  )
     8  
     9  var ErrWireTypeUnknown = errors.New("unknown wire type")
    10  
    11  type UnmarshalFieldError struct {
    12  	FieldNumber int
    13  	WireType    int
    14  	Err         error
    15  }
    16  
    17  func (e *UnmarshalFieldError) Error() string {
    18  	return fmt.Sprintf("field number %d with wire type %d: %v", e.FieldNumber, e.WireType, e.Err)
    19  }
    20  
    21  func (e *UnmarshalFieldError) Unwrap() error { return e.Err }
    22  
    23  func fieldError(f fieldNumber, t wireType, err error) error {
    24  	return &UnmarshalFieldError{
    25  		FieldNumber: int(f),
    26  		WireType:    int(t),
    27  		Err:         err,
    28  	}
    29  }
    30  
    31  // An InvalidUnmarshalError describes an invalid argument passed to Unmarshal.
    32  // (The argument to Unmarshal must be a non-nil pointer to a struct.)
    33  type InvalidUnmarshalError struct {
    34  	Type reflect.Type
    35  }
    36  
    37  func (e *InvalidUnmarshalError) Error() string {
    38  	if e.Type == nil {
    39  		return "proto: Unmarshal(nil)"
    40  	}
    41  
    42  	if e.Type.Kind() != reflect.Pointer {
    43  		return "proto: Unmarshal(non-pointer" + e.Type.String() + ")"
    44  	}
    45  
    46  	elem := e.Type.Elem()
    47  	if elem.Kind() != reflect.Struct {
    48  		return "proto: Unmarshal(bad pointer" + e.Type.String() + ")"
    49  	}
    50  	return "proto: Unmarshal(nil " + e.Type.String() + ")"
    51  }