github.com/decred/dcrlnd@v0.7.6/watchtower/wtwire/error.go (about)

     1  package wtwire
     2  
     3  import "io"
     4  
     5  // Error is a generic error message that can be sent to a client if a request
     6  // fails outside of prescribed protocol errors. Typically this would be followed
     7  // by the server disconnecting the client, and so can be useful to transferring
     8  // the exact reason.
     9  type Error struct {
    10  	// Code specifies the error code encountered by the server.
    11  	Code ErrorCode
    12  
    13  	// Data encodes a payload whose contents can be interpreted by the
    14  	// client in response to the error code.
    15  	Data []byte
    16  }
    17  
    18  // NewError returns an freshly-initialized Error message.
    19  func NewError() *Error {
    20  	return &Error{}
    21  }
    22  
    23  // A compile time check to ensure Error implements the wtwire.Message interface.
    24  var _ Message = (*Error)(nil)
    25  
    26  // Decode deserializes a serialized Error message stored in the passed io.Reader
    27  // observing the specified protocol version.
    28  //
    29  // This is part of the wtwire.Message interface.
    30  func (e *Error) Decode(r io.Reader, pver uint32) error {
    31  	return ReadElements(r,
    32  		&e.Code,
    33  		&e.Data,
    34  	)
    35  }
    36  
    37  // Encode serializes the target Error into the passed io.Writer observing the
    38  // protocol version specified.
    39  //
    40  // This is part of the wtwire.Message interface.
    41  func (e *Error) Encode(w io.Writer, prver uint32) error {
    42  	return WriteElements(w,
    43  		e.Code,
    44  		e.Data,
    45  	)
    46  }
    47  
    48  // MsgType returns the integer uniquely identifying this message type on the
    49  // wire.
    50  //
    51  // This is part of the wtwire.Message interface.
    52  func (e *Error) MsgType() MessageType {
    53  	return MsgError
    54  }
    55  
    56  // MaxPayloadLength returns the maximum allowed payload size for a Error
    57  // complete message observing the specified protocol version.
    58  //
    59  // This is part of the wtwire.Message interface.
    60  func (e *Error) MaxPayloadLength(uint32) uint32 {
    61  	return MaxMessagePayload
    62  }