Skip to content

Commit dc439ad

Browse files
Implement writeable for APIError
1 parent 9d2b8f7 commit dc439ad

File tree

4 files changed

+21
-9
lines changed

4 files changed

+21
-9
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2369,10 +2369,10 @@ where
23692369
let session_priv = SecretKey::from_slice(&session_priv_bytes[..]).expect("RNG is busted");
23702370

23712371
let onion_keys = onion_utils::construct_onion_keys(&self.secp_ctx, &path, &session_priv)
2372-
.map_err(|_| APIError::InvalidRoute{err: "Pubkey along hop was maliciously selected"})?;
2372+
.map_err(|_| APIError::InvalidRoute{err: "Pubkey along hop was maliciously selected".to_string()})?;
23732373
let (onion_payloads, htlc_msat, htlc_cltv) = onion_utils::build_onion_payloads(path, total_value, payment_secret, cur_height, keysend_preimage)?;
23742374
if onion_utils::route_size_insane(&onion_payloads) {
2375-
return Err(APIError::InvalidRoute{err: "Route size too large considering onion data"});
2375+
return Err(APIError::InvalidRoute{err: "Route size too large considering onion data".to_string()});
23762376
}
23772377
let onion_packet = onion_utils::construct_onion_packet(onion_payloads, onion_keys, prng_seed, payment_hash);
23782378

@@ -2386,7 +2386,7 @@ where
23862386

23872387
let per_peer_state = self.per_peer_state.read().unwrap();
23882388
let peer_state_mutex = per_peer_state.get(&counterparty_node_id)
2389-
.ok_or_else(|| APIError::InvalidRoute{err: "No peer matching the path's first hop found!" })?;
2389+
.ok_or_else(|| APIError::InvalidRoute{err: "No peer matching the path's first hop found!".to_string() })?;
23902390
let mut peer_state_lock = peer_state_mutex.lock().unwrap();
23912391
let peer_state = &mut *peer_state_lock;
23922392
if let hash_map::Entry::Occupied(mut chan) = peer_state.channel_by_id.entry(id) {

lightning/src/ln/onion_utils.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,11 +182,11 @@ pub(super) fn build_onion_payloads(path: &Vec<RouteHop>, total_msat: u64, paymen
182182
});
183183
cur_value_msat += hop.fee_msat;
184184
if cur_value_msat >= 21000000 * 100000000 * 1000 {
185-
return Err(APIError::InvalidRoute{err: "Channel fees overflowed?"});
185+
return Err(APIError::InvalidRoute{err: "Channel fees overflowed?".to_string()});
186186
}
187187
cur_cltv += hop.cltv_expiry_delta as u32;
188188
if cur_cltv >= 500000000 {
189-
return Err(APIError::InvalidRoute{err: "Channel CLTV overflowed?"});
189+
return Err(APIError::InvalidRoute{err: "Channel CLTV overflowed?".to_string()});
190190
}
191191
last_short_channel_id = hop.short_channel_id;
192192
}

lightning/src/ln/outbound_payment.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -856,7 +856,7 @@ impl OutboundPayments {
856856
u32, PaymentId, &Option<PaymentPreimage>, [u8; 32]) -> Result<(), APIError>
857857
{
858858
if route.paths.len() < 1 {
859-
return Err(PaymentSendFailure::ParameterError(APIError::InvalidRoute{err: "There must be at least one path to send over"}));
859+
return Err(PaymentSendFailure::ParameterError(APIError::InvalidRoute{err: "There must be at least one path to send over".to_string()}));
860860
}
861861
if payment_secret.is_none() && route.paths.len() > 1 {
862862
return Err(PaymentSendFailure::ParameterError(APIError::APIMisuseError{err: "Payment secret is required for multi-path payments".to_string()}));
@@ -866,12 +866,12 @@ impl OutboundPayments {
866866
let mut path_errs = Vec::with_capacity(route.paths.len());
867867
'path_check: for path in route.paths.iter() {
868868
if path.len() < 1 || path.len() > 20 {
869-
path_errs.push(Err(APIError::InvalidRoute{err: "Path didn't go anywhere/had bogus size"}));
869+
path_errs.push(Err(APIError::InvalidRoute{err: "Path didn't go anywhere/had bogus size".to_string()}));
870870
continue 'path_check;
871871
}
872872
for (idx, hop) in path.iter().enumerate() {
873873
if idx != path.len() - 1 && hop.pubkey == our_node_id {
874-
path_errs.push(Err(APIError::InvalidRoute{err: "Path went through us but wasn't a simple rebalance loop to us"}));
874+
path_errs.push(Err(APIError::InvalidRoute{err: "Path went through us but wasn't a simple rebalance loop to us".to_string()}));
875875
continue 'path_check;
876876
}
877877
}

lightning/src/util/errors.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ pub enum APIError {
3737
/// too-many-hops, etc).
3838
InvalidRoute {
3939
/// A human-readable error message
40-
err: &'static str
40+
err: String
4141
},
4242
/// We were unable to complete the request as the Channel required to do so is unable to
4343
/// complete the request (or was not found). This can take many forms, including disconnected
@@ -84,6 +84,18 @@ impl fmt::Debug for APIError {
8484
}
8585
}
8686

87+
impl_writeable_tlv_based_enum_upgradable!(APIError,
88+
(0, APIMisuseError) => { (0, err, required), },
89+
(2, FeeRateTooHigh) => {
90+
(0, err, required),
91+
(2, feerate, required),
92+
},
93+
(4, InvalidRoute) => { (0, err, required), },
94+
(6, ChannelUnavailable) => { (0, err, required), },
95+
(8, MonitorUpdateInProgress) => {},
96+
(10, IncompatibleShutdownScript) => { (0, script, required), },
97+
);
98+
8799
#[inline]
88100
pub(crate) fn get_onion_debug_field(error_code: u16) -> (&'static str, usize) {
89101
match error_code & 0xff {

0 commit comments

Comments
 (0)