github.com/decred/dcrlnd@v0.7.6/lnwire/error.go (about) 1 package lnwire 2 3 import ( 4 "bytes" 5 "fmt" 6 "io" 7 ) 8 9 // FundingError represents a set of errors that can be encountered and sent 10 // during the funding workflow. 11 type FundingError uint8 12 13 const ( 14 // ErrMaxPendingChannels is returned by remote peer when the number of 15 // active pending channels exceeds their maximum policy limit. 16 ErrMaxPendingChannels FundingError = 1 17 18 // ErrSynchronizingChain is returned by a remote peer that receives a 19 // channel update or a funding request while it's still syncing to the 20 // latest state of the blockchain. 21 ErrSynchronizingChain FundingError = 2 22 23 // ErrChanTooLarge is returned by a remote peer that receives a 24 // FundingOpen request for a channel that is above their current 25 // soft-limit. 26 ErrChanTooLarge FundingError = 3 27 ) 28 29 // String returns a human readable version of the target FundingError. 30 func (e FundingError) String() string { 31 switch e { 32 case ErrMaxPendingChannels: 33 return "Number of pending channels exceed maximum" 34 case ErrSynchronizingChain: 35 return "Synchronizing blockchain" 36 case ErrChanTooLarge: 37 return "channel too large" 38 default: 39 return "unknown error" 40 } 41 } 42 43 // Error returns the human readable version of the target FundingError. 44 // 45 // NOTE: Satisfies the Error interface. 46 func (e FundingError) Error() string { 47 return e.String() 48 } 49 50 // ErrorData is a set of bytes associated with a particular sent error. A 51 // receiving node SHOULD only print out data verbatim if the string is composed 52 // solely of printable ASCII characters. For reference, the printable character 53 // set includes byte values 32 through 127 inclusive. 54 type ErrorData []byte 55 56 // Error represents a generic error bound to an exact channel. The message 57 // format is purposefully general in order to allow expression of a wide array 58 // of possible errors. Each Error message is directed at a particular open 59 // channel referenced by ChannelPoint. 60 type Error struct { 61 // ChanID references the active channel in which the error occurred 62 // within. If the ChanID is all zeros, then this error applies to the 63 // entire established connection. 64 ChanID ChannelID 65 66 // Data is the attached error data that describes the exact failure 67 // which caused the error message to be sent. 68 Data ErrorData 69 } 70 71 // NewError creates a new Error message. 72 func NewError() *Error { 73 return &Error{} 74 } 75 76 // A compile time check to ensure Error implements the lnwire.Message 77 // interface. 78 var _ Message = (*Error)(nil) 79 80 // Error returns the string representation to Error. 81 // 82 // NOTE: Satisfies the error interface. 83 func (c *Error) Error() string { 84 errMsg := "non-ascii data" 85 if isASCII(c.Data) { 86 errMsg = string(c.Data) 87 } 88 89 return fmt.Sprintf("chan_id=%v, err=%v", c.ChanID, errMsg) 90 } 91 92 // Decode deserializes a serialized Error message stored in the passed 93 // io.Reader observing the specified protocol version. 94 // 95 // This is part of the lnwire.Message interface. 96 func (c *Error) Decode(r io.Reader, pver uint32) error { 97 return ReadElements(r, 98 &c.ChanID, 99 &c.Data, 100 ) 101 } 102 103 // Encode serializes the target Error into the passed io.Writer observing the 104 // protocol version specified. 105 // 106 // This is part of the lnwire.Message interface. 107 func (c *Error) Encode(w *bytes.Buffer, pver uint32) error { 108 if err := WriteBytes(w, c.ChanID[:]); err != nil { 109 return err 110 } 111 112 return WriteErrorData(w, c.Data) 113 } 114 115 // MsgType returns the integer uniquely identifying an Error message on the 116 // wire. 117 // 118 // This is part of the lnwire.Message interface. 119 func (c *Error) MsgType() MessageType { 120 return MsgError 121 } 122 123 // isASCII is a helper method that checks whether all bytes in `data` would be 124 // printable ASCII characters if interpreted as a string. 125 func isASCII(data []byte) bool { 126 for _, c := range data { 127 if c < 32 || c > 126 { 128 return false 129 } 130 } 131 return true 132 }