From 999466c860f75bb2cd83f0fc9c3258691ffc549a Mon Sep 17 00:00:00 2001 From: Conner Fromknecht Date: Tue, 23 Oct 2018 18:28:22 -0700 Subject: [PATCH] watchtower/wtwire/error: add generic error message --- watchtower/wtwire/error.go | 62 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 watchtower/wtwire/error.go diff --git a/watchtower/wtwire/error.go b/watchtower/wtwire/error.go new file mode 100644 index 00000000..c8d61fa0 --- /dev/null +++ b/watchtower/wtwire/error.go @@ -0,0 +1,62 @@ +package wtwire + +import "io" + +// Error is a generic error message that can be sent to a client if a request +// fails outside of prescribed protocol errors. Typically this would be followed +// by the server disconnecting the client, and so can be useful to transfering +// the exact reason. +type Error struct { + // Code specifies the error code encountered by the server. + Code ErrorCode + + // Data encodes a payload whose contents can be interpreted by the + // client in response to the error code. + Data []byte +} + +// NewError returns an freshly-initialized Error message. +func NewError() *Error { + return &Error{} +} + +// A compile time check to ensure Error implements the wtwire.Message interface. +var _ Message = (*Error)(nil) + +// Decode deserializes a serialized Error message stored in the passed io.Reader +// observing the specified protocol version. +// +// This is part of the wtwire.Message interface. +func (e *Error) Decode(r io.Reader, pver uint32) error { + return ReadElements(r, + &e.Code, + &e.Data, + ) +} + +// Encode serializes the target Error into the passed io.Writer observing the +// protocol version specified. +// +// This is part of the wtwire.Message interface. +func (e *Error) Encode(w io.Writer, prver uint32) error { + return WriteElements(w, + e.Code, + e.Data, + ) +} + +// MsgType returns the integer uniquely identifying this message type on the +// wire. +// +// This is part of the wtwire.Message interface. +func (e *Error) MsgType() MessageType { + return MsgError +} + +// MaxPayloadLength returns the maximum allowed payload size for a Error +// complete message observing the specified protocol version. +// +// This is part of the wtwire.Message interface. +func (e *Error) MaxPayloadLength(uint32) uint32 { + return MaxMessagePayload +}