lnwire+multi: define Error() for lnwire.Error

To make lnwire.Error actually satisfy the error interface, define the
Error method directly.
This commit is contained in:
Johan T. Halseth 2019-09-20 10:55:21 +02:00
parent 949f6c6cec
commit 33fe09482b
No known key found for this signature in database
GPG Key ID: 15BAADA29DA20D26
5 changed files with 41 additions and 34 deletions

@ -2955,14 +2955,14 @@ func (f *fundingManager) handleErrorMsg(fmsg *fundingErrorMsg) {
resCtx, err := f.cancelReservationCtx(fmsg.peerKey, chanID) resCtx, err := f.cancelReservationCtx(fmsg.peerKey, chanID)
if err != nil { if err != nil {
fndgLog.Warnf("Received error for non-existent funding "+ fndgLog.Warnf("Received error for non-existent funding "+
"flow: %v (%v)", err, spew.Sdump(protocolErr)) "flow: %v (%v)", err, protocolErr.Error())
return return
} }
// If we did indeed find the funding workflow, then we'll return the // If we did indeed find the funding workflow, then we'll return the
// error back to the caller (if any), and cancel the workflow itself. // error back to the caller (if any), and cancel the workflow itself.
fundingErr := fmt.Errorf("received funding error from %x: %v", fundingErr := fmt.Errorf("received funding error from %x: %v",
fmsg.peerKey.SerializeCompressed(), string(protocolErr.Data), fmsg.peerKey.SerializeCompressed(), protocolErr.Error(),
) )
fndgLog.Errorf(fundingErr.Error()) fndgLog.Errorf(fundingErr.Error())

@ -602,7 +602,7 @@ func fundChannel(t *testing.T, alice, bob *testNode, localFundingAmt,
if gotError { if gotError {
t.Fatalf("expected OpenChannel to be sent "+ t.Fatalf("expected OpenChannel to be sent "+
"from bob, instead got error: %v", "from bob, instead got error: %v",
lnwire.ErrorCode(errorMsg.Data[0])) errorMsg.Error())
} }
t.Fatalf("expected OpenChannel to be sent from "+ t.Fatalf("expected OpenChannel to be sent from "+
"alice, instead got %T", aliceMsg) "alice, instead got %T", aliceMsg)
@ -728,7 +728,7 @@ func assertFundingMsgSent(t *testing.T, msgChan chan lnwire.Message,
errorMsg, gotError := msg.(*lnwire.Error) errorMsg, gotError := msg.(*lnwire.Error)
if gotError { if gotError {
t.Fatalf("expected %s to be sent, instead got error: %v", t.Fatalf("expected %s to be sent, instead got error: %v",
msgType, lnwire.ErrorCode(errorMsg.Data[0])) msgType, errorMsg.Error())
} }
_, _, line, _ := runtime.Caller(1) _, _, line, _ := runtime.Caller(1)
@ -1469,7 +1469,7 @@ func TestFundingManagerPeerTimeoutAfterInitFunding(t *testing.T) {
if gotError { if gotError {
t.Fatalf("expected OpenChannel to be sent "+ t.Fatalf("expected OpenChannel to be sent "+
"from bob, instead got error: %v", "from bob, instead got error: %v",
lnwire.ErrorCode(errorMsg.Data[0])) errorMsg.Error())
} }
t.Fatalf("expected OpenChannel to be sent from "+ t.Fatalf("expected OpenChannel to be sent from "+
"alice, instead got %T", aliceMsg) "alice, instead got %T", aliceMsg)
@ -1531,7 +1531,7 @@ func TestFundingManagerPeerTimeoutAfterFundingOpen(t *testing.T) {
if gotError { if gotError {
t.Fatalf("expected OpenChannel to be sent "+ t.Fatalf("expected OpenChannel to be sent "+
"from bob, instead got error: %v", "from bob, instead got error: %v",
lnwire.ErrorCode(errorMsg.Data[0])) errorMsg.Error())
} }
t.Fatalf("expected OpenChannel to be sent from "+ t.Fatalf("expected OpenChannel to be sent from "+
"alice, instead got %T", aliceMsg) "alice, instead got %T", aliceMsg)
@ -1602,7 +1602,7 @@ func TestFundingManagerPeerTimeoutAfterFundingAccept(t *testing.T) {
if gotError { if gotError {
t.Fatalf("expected OpenChannel to be sent "+ t.Fatalf("expected OpenChannel to be sent "+
"from bob, instead got error: %v", "from bob, instead got error: %v",
lnwire.ErrorCode(errorMsg.Data[0])) errorMsg.Error())
} }
t.Fatalf("expected OpenChannel to be sent from "+ t.Fatalf("expected OpenChannel to be sent from "+
"alice, instead got %T", aliceMsg) "alice, instead got %T", aliceMsg)
@ -2326,7 +2326,7 @@ func TestFundingManagerCustomChannelParameters(t *testing.T) {
if gotError { if gotError {
t.Fatalf("expected OpenChannel to be sent "+ t.Fatalf("expected OpenChannel to be sent "+
"from bob, instead got error: %v", "from bob, instead got error: %v",
lnwire.ErrorCode(errorMsg.Data[0])) errorMsg.Error())
} }
t.Fatalf("expected OpenChannel to be sent from "+ t.Fatalf("expected OpenChannel to be sent from "+
"alice, instead got %T", aliceMsg) "alice, instead got %T", aliceMsg)
@ -2561,7 +2561,7 @@ func TestFundingManagerMaxPendingChannels(t *testing.T) {
if gotError { if gotError {
t.Fatalf("expected OpenChannel to be sent "+ t.Fatalf("expected OpenChannel to be sent "+
"from bob, instead got error: %v", "from bob, instead got error: %v",
lnwire.ErrorCode(errorMsg.Data[0])) errorMsg.Error())
} }
t.Fatalf("expected OpenChannel to be sent from "+ t.Fatalf("expected OpenChannel to be sent from "+
"alice, instead got %T", aliceMsg) "alice, instead got %T", aliceMsg)
@ -2725,7 +2725,7 @@ func TestFundingManagerRejectPush(t *testing.T) {
if gotError { if gotError {
t.Fatalf("expected OpenChannel to be sent "+ t.Fatalf("expected OpenChannel to be sent "+
"from bob, instead got error: %v", "from bob, instead got error: %v",
lnwire.ErrorCode(errorMsg.Data[0])) errorMsg.Error())
} }
t.Fatalf("expected OpenChannel to be sent from "+ t.Fatalf("expected OpenChannel to be sent from "+
"alice, instead got %T", aliceMsg) "alice, instead got %T", aliceMsg)
@ -2736,9 +2736,9 @@ func TestFundingManagerRejectPush(t *testing.T) {
// Assert Bob responded with an ErrNonZeroPushAmount error. // Assert Bob responded with an ErrNonZeroPushAmount error.
err := assertFundingMsgSent(t, bob.msgChan, "Error").(*lnwire.Error) err := assertFundingMsgSent(t, bob.msgChan, "Error").(*lnwire.Error)
if "Non-zero push amounts are disabled" != string(err.Data) { if !strings.Contains(err.Error(), "Non-zero push amounts are disabled") {
t.Fatalf("expected ErrNonZeroPushAmount error, got \"%v\"", t.Fatalf("expected ErrNonZeroPushAmount error, got \"%v\"",
string(err.Data)) err.Error())
} }
} }
@ -2782,7 +2782,7 @@ func TestFundingManagerMaxConfs(t *testing.T) {
if gotError { if gotError {
t.Fatalf("expected OpenChannel to be sent "+ t.Fatalf("expected OpenChannel to be sent "+
"from bob, instead got error: %v", "from bob, instead got error: %v",
lnwire.ErrorCode(errorMsg.Data[0])) errorMsg.Error())
} }
t.Fatalf("expected OpenChannel to be sent from "+ t.Fatalf("expected OpenChannel to be sent from "+
"alice, instead got %T", aliceMsg) "alice, instead got %T", aliceMsg)
@ -2805,9 +2805,9 @@ func TestFundingManagerMaxConfs(t *testing.T) {
// Alice should respond back with an error indicating MinAcceptDepth is // Alice should respond back with an error indicating MinAcceptDepth is
// too large. // too large.
err := assertFundingMsgSent(t, alice.msgChan, "Error").(*lnwire.Error) err := assertFundingMsgSent(t, alice.msgChan, "Error").(*lnwire.Error)
if !strings.Contains(string(err.Data), "minimum depth") { if !strings.Contains(err.Error(), "minimum depth") {
t.Fatalf("expected ErrNumConfsTooLarge, got \"%v\"", t.Fatalf("expected ErrNumConfsTooLarge, got \"%v\"",
string(err.Data)) err.Error())
} }
} }

@ -1882,13 +1882,9 @@ func (l *channelLink) handleUpstreamMsg(msg lnwire.Message) {
// Error received from remote, MUST fail channel, but should // Error received from remote, MUST fail channel, but should
// only print the contents of the error message if all // only print the contents of the error message if all
// characters are printable ASCII. // characters are printable ASCII.
errMsg := "non-ascii data"
if isASCII(msg.Data) {
errMsg = string(msg.Data)
}
l.fail(LinkFailureError{code: ErrRemoteError}, l.fail(LinkFailureError{code: ErrRemoteError},
"ChannelPoint(%v): received error from peer: %v", "ChannelPoint(%v): received error from peer: %v",
l.channel.ChannelPoint(), errMsg) l.channel.ChannelPoint(), msg.Error())
default: default:
log.Warnf("ChannelPoint(%v): received unknown message of type %T", log.Warnf("ChannelPoint(%v): received unknown message of type %T",
l.channel.ChannelPoint(), msg) l.channel.ChannelPoint(), msg)
@ -3100,16 +3096,3 @@ func (l *channelLink) tracef(format string, a ...interface{}) {
msg := fmt.Sprintf(format, a...) msg := fmt.Sprintf(format, a...)
log.Tracef("ChannelLink(%s) %s", l.ShortChanID(), msg) log.Tracef("ChannelLink(%s) %s", l.ShortChanID(), msg)
} }
// isASCII is a helper method that checks whether all bytes in `data` would be
// printable ASCII characters if interpreted as a string.
func isASCII(data []byte) bool {
isASCII := true
for _, c := range data {
if c < 32 || c > 126 {
isASCII = false
break
}
}
return isASCII
}

@ -1,6 +1,7 @@
package lnwire package lnwire
import ( import (
"fmt"
"io" "io"
"google.golang.org/grpc/codes" "google.golang.org/grpc/codes"
@ -87,6 +88,18 @@ func NewError() *Error {
// interface. // interface.
var _ Message = (*Error)(nil) var _ Message = (*Error)(nil)
// Error returns the string representation to Error.
//
// NOTE: Satisfies the error interface.
func (c *Error) Error() string {
errMsg := "non-ascii data"
if isASCII(c.Data) {
errMsg = string(c.Data)
}
return fmt.Sprintf("chan_id=%v, err=%v", c.ChanID, errMsg)
}
// Decode deserializes a serialized Error message stored in the passed // Decode deserializes a serialized Error message stored in the passed
// io.Reader observing the specified protocol version. // io.Reader observing the specified protocol version.
// //
@ -125,3 +138,14 @@ func (c *Error) MaxPayloadLength(uint32) uint32 {
// 32 + 2 + 65501 // 32 + 2 + 65501
return 65535 return 65535
} }
// isASCII is a helper method that checks whether all bytes in `data` would be
// printable ASCII characters if interpreted as a string.
func isASCII(data []byte) bool {
for _, c := range data {
if c < 32 || c > 126 {
return false
}
}
return true
}

@ -1250,7 +1250,7 @@ func messageSummary(msg lnwire.Message) string {
msg.ChanID, msg.ID, msg.FailureCode) msg.ChanID, msg.ID, msg.FailureCode)
case *lnwire.Error: case *lnwire.Error:
return fmt.Sprintf("chan_id=%v, err=%v", msg.ChanID, string(msg.Data)) return fmt.Sprintf("%v", msg.Error())
case *lnwire.AnnounceSignatures: case *lnwire.AnnounceSignatures:
return fmt.Sprintf("chan_id=%v, short_chan_id=%v", msg.ChannelID, return fmt.Sprintf("chan_id=%v, short_chan_id=%v", msg.ChannelID,