github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/network/codec/errors.go (about)

     1  package codec
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  )
     7  
     8  // ErrInvalidEncoding indicates that the message code byte (first byte of message payload) is unknown.
     9  type ErrInvalidEncoding struct {
    10  	err error
    11  }
    12  
    13  func (e ErrInvalidEncoding) Error() string {
    14  	return fmt.Sprintf("failed to decode message with invalid encoding: %v", e.err)
    15  }
    16  
    17  // NewInvalidEncodingErr returns a new ErrInvalidEncoding
    18  func NewInvalidEncodingErr(err error) ErrInvalidEncoding {
    19  	return ErrInvalidEncoding{err}
    20  }
    21  
    22  // IsErrInvalidEncoding returns true if an error is ErrInvalidEncoding
    23  func IsErrInvalidEncoding(err error) bool {
    24  	var e ErrInvalidEncoding
    25  	return errors.As(err, &e)
    26  }
    27  
    28  // ErrUnknownMsgCode indicates that the message code byte (first byte of message payload) is unknown.
    29  type ErrUnknownMsgCode struct {
    30  	code MessageCode
    31  }
    32  
    33  func (e ErrUnknownMsgCode) Error() string {
    34  	return fmt.Sprintf("failed to decode message could not get interface from unknown message code: %d", e.code)
    35  }
    36  
    37  // NewUnknownMsgCodeErr returns a new ErrUnknownMsgCode
    38  func NewUnknownMsgCodeErr(code MessageCode) ErrUnknownMsgCode {
    39  	return ErrUnknownMsgCode{code}
    40  }
    41  
    42  // IsErrUnknownMsgCode returns true if an error is ErrUnknownMsgCode
    43  func IsErrUnknownMsgCode(err error) bool {
    44  	var e ErrUnknownMsgCode
    45  	return errors.As(err, &e)
    46  }
    47  
    48  // ErrMsgUnmarshal indicates that the message could not be unmarshalled.
    49  type ErrMsgUnmarshal struct {
    50  	code    uint8
    51  	msgType string
    52  	err     string
    53  }
    54  
    55  func (e ErrMsgUnmarshal) Error() string {
    56  	return fmt.Sprintf("failed to unmarshal message payload with message type %s and message code %d: %s", e.msgType, e.code, e.err)
    57  }
    58  
    59  // NewMsgUnmarshalErr returns a new ErrMsgUnmarshal
    60  func NewMsgUnmarshalErr(code uint8, msgType string, err error) ErrMsgUnmarshal {
    61  	return ErrMsgUnmarshal{code: code, msgType: msgType, err: err.Error()}
    62  }
    63  
    64  // IsErrMsgUnmarshal returns true if an error is ErrMsgUnmarshal
    65  func IsErrMsgUnmarshal(err error) bool {
    66  	var e ErrMsgUnmarshal
    67  	return errors.As(err, &e)
    68  }