Skip to content

Commit d64e736

Browse files
committed
Use SemanticError in OfferBuilder::build
1 parent 28f1312 commit d64e736

File tree

2 files changed

+12
-11
lines changed

2 files changed

+12
-11
lines changed

lightning/src/offers/offer.rs

+10-11
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,7 @@
4949
//! .issuer("Foo Bar".to_string())
5050
//! .path(create_blinded_path())
5151
//! .path(create_another_blinded_path())
52-
//! .build()
53-
//! .unwrap();
52+
//! .build()?;
5453
//!
5554
//! // Encode as a bech32 string for use in a QR code.
5655
//! let encoded_offer = offer.to_string();
@@ -228,15 +227,15 @@ impl OfferBuilder {
228227
}
229228

230229
/// Builds an [`Offer`] from the builder's settings.
231-
pub fn build(self) -> Result<Offer, ()> {
230+
pub fn build(self) -> Result<Offer, SemanticError> {
232231
// TODO: Also check for Amount::Currency
233232
if let Some(Amount::Currency { .. }) = self.offer.amount {
234233
} else if self.offer.amount_msats() > MAX_VALUE_MSAT {
235-
return Err(());
234+
return Err(SemanticError::InvalidAmount);
236235
}
237236

238237
if self.offer.quantity_min() > self.offer.quantity_max() {
239-
return Err(());
238+
return Err(SemanticError::InvalidQuantity);
240239
}
241240

242241
let mut bytes = Vec::new();
@@ -589,6 +588,7 @@ mod tests {
589588
use core::time::Duration;
590589
use ln::features::OfferFeatures;
591590
use ln::msgs::MAX_VALUE_MSAT;
591+
use offers::parse::SemanticError;
592592
use onion_message::{BlindedHop, BlindedPath};
593593
use util::ser::Writeable;
594594
use util::string::PrintableString;
@@ -727,7 +727,7 @@ mod tests {
727727
let invalid_amount = Amount::Bitcoin { amount_msats: MAX_VALUE_MSAT + 1 };
728728
match OfferBuilder::new("foo".into(), pubkey(42)).amount(invalid_amount).build() {
729729
Ok(_) => panic!("expected error"),
730-
Err(e) => assert_eq!(e, ()),
730+
Err(e) => assert_eq!(e, SemanticError::InvalidAmount),
731731
}
732732
}
733733

@@ -953,11 +953,10 @@ mod tests {
953953
assert_eq!(tlv_stream.quantity_min, None);
954954
assert_eq!(tlv_stream.quantity_max, Some(9));
955955

956-
assert!(OfferBuilder::new("foo".into(), pubkey(42))
957-
.quantity_range(ten..five)
958-
.build()
959-
.is_err()
960-
);
956+
match OfferBuilder::new("foo".into(), pubkey(42)).quantity_range(ten..five).build() {
957+
Ok(_) => panic!("expected error"),
958+
Err(e) => assert_eq!(e, SemanticError::InvalidQuantity),
959+
}
961960
}
962961
}
963962

lightning/src/offers/parse.rs

+2
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ pub enum ParseError {
7474
pub enum SemanticError {
7575
/// An amount was expected but was missing.
7676
MissingAmount,
77+
/// An amount exceeded the maximum number of bitcoin.
78+
InvalidAmount,
7779
/// A required description was not provided.
7880
MissingDescription,
7981
/// A node id was not provided.

0 commit comments

Comments
 (0)