routerrpc: extract wire error marshalling

Preparation for marshalling wire errors as part of rpc payment lookups.
This commit is contained in:
Joost Jager 2020-02-19 17:20:43 +01:00
parent 3f5ba35dea
commit e6e9e44e6f
No known key found for this signature in database
GPG Key ID: A61B9D4C393C59C7

@ -904,7 +904,35 @@ func marshallError(sendError error) (*lnrpc.Failure, error) {
return nil, sendError return nil, sendError
} }
switch onionErr := rtErr.WireMessage().(type) { err := marshallWireError(rtErr.WireMessage(), response)
if err != nil {
return nil, err
}
// If the ClearTextError received is a ForwardingError, the error
// originated from a node along the route, not locally on our outgoing
// link. We set failureSourceIdx to the index of the node where the
// failure occurred. If the error is not a ForwardingError, the failure
// occurred at our node, so we leave the index as 0 to indicate that
// we failed locally.
fErr, ok := rtErr.(*htlcswitch.ForwardingError)
if ok {
response.FailureSourceIndex = uint32(fErr.FailureSourceIdx)
}
return response, nil
}
// marshallError marshall an error as received from the switch to rpc structs
// suitable for returning to the caller of an rpc method.
//
// Because of difficulties with using protobuf oneof constructs in some
// languages, the decision was made here to use a single message format for all
// failure messages with some fields left empty depending on the failure type.
func marshallWireError(msg lnwire.FailureMessage,
response *lnrpc.Failure) error {
switch onionErr := msg.(type) {
case *lnwire.FailIncorrectDetails: case *lnwire.FailIncorrectDetails:
response.Code = lnrpc.Failure_INCORRECT_OR_UNKNOWN_PAYMENT_DETAILS response.Code = lnrpc.Failure_INCORRECT_OR_UNKNOWN_PAYMENT_DETAILS
@ -995,21 +1023,10 @@ func marshallError(sendError error) (*lnrpc.Failure, error) {
response.Code = lnrpc.Failure_UNKNOWN_FAILURE response.Code = lnrpc.Failure_UNKNOWN_FAILURE
default: default:
return nil, fmt.Errorf("cannot marshall failure %T", onionErr) return fmt.Errorf("cannot marshall failure %T", onionErr)
} }
// If the ClearTextError received is a ForwardingError, the error return nil
// originated from a node along the route, not locally on our outgoing
// link. We set failureSourceIdx to the index of the node where the
// failure occurred. If the error is not a ForwardingError, the failure
// occurred at our node, so we leave the index as 0 to indicate that
// we failed locally.
fErr, ok := rtErr.(*htlcswitch.ForwardingError)
if ok {
response.FailureSourceIndex = uint32(fErr.FailureSourceIdx)
}
return response, nil
} }
// marshallChannelUpdate marshalls a channel update as received over the wire to // marshallChannelUpdate marshalls a channel update as received over the wire to