Skip to content

Commit a2cc78b

Browse files
committed
Use SemanticError in OfferBuilder::build
1 parent dabee95 commit a2cc78b

File tree

2 files changed

+12
-11
lines changed

2 files changed

+12
-11
lines changed

lightning/src/offers/offer.rs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,7 @@
4747
//! .issuer("Foo Bar".to_string())
4848
//! .path(create_blinded_path())
4949
//! .path(create_another_blinded_path())
50-
//! .build()
51-
//! .unwrap();
50+
//! .build()?;
5251
//!
5352
//! // Encode as a bech32 string for use in a QR code.
5453
//! let encoded_offer = offer.to_string();
@@ -218,15 +217,15 @@ impl OfferBuilder {
218217
}
219218

220219
/// Builds an [`Offer`] from the builder's settings.
221-
pub fn build(self) -> Result<Offer, ()> {
220+
pub fn build(self) -> Result<Offer, SemanticError> {
222221
// TODO: Also check for Amount::Currency
223222
if let Some(Amount::Currency { .. }) = self.offer.amount {
224223
} else if self.offer.amount_msats() > MAX_VALUE_MSAT {
225-
return Err(());
224+
return Err(SemanticError::InvalidAmount);
226225
}
227226

228227
if self.offer.quantity_min() > self.offer.quantity_max() {
229-
return Err(());
228+
return Err(SemanticError::InvalidQuantity);
230229
}
231230

232231
let mut bytes = Vec::new();
@@ -554,6 +553,7 @@ mod tests {
554553
use core::time::Duration;
555554
use ln::features::OfferFeatures;
556555
use ln::msgs::MAX_VALUE_MSAT;
556+
use offers::parse::SemanticError;
557557
use onion_message::{BlindedHop, BlindedPath};
558558
use util::ser::Writeable;
559559

@@ -687,7 +687,7 @@ mod tests {
687687
let invalid_amount = Amount::Bitcoin { amount_msats: MAX_VALUE_MSAT + 1 };
688688
match OfferBuilder::new("foo".into(), pubkey(42)).amount(invalid_amount).build() {
689689
Ok(_) => panic!("expected error"),
690-
Err(e) => assert_eq!(e, ()),
690+
Err(e) => assert_eq!(e, SemanticError::InvalidAmount),
691691
}
692692
}
693693

@@ -913,11 +913,10 @@ mod tests {
913913
assert_eq!(tlv_stream.quantity_min, None);
914914
assert_eq!(tlv_stream.quantity_max, Some(9));
915915

916-
assert!(OfferBuilder::new("foo".into(), pubkey(42))
917-
.quantity_range(ten..five)
918-
.build()
919-
.is_err()
920-
);
916+
match OfferBuilder::new("foo".into(), pubkey(42)).quantity_range(ten..five).build() {
917+
Ok(_) => panic!("expected error"),
918+
Err(e) => assert_eq!(e, SemanticError::InvalidQuantity),
919+
}
921920
}
922921
}
923922

lightning/src/offers/parse.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ pub enum ParseError {
7777
pub enum SemanticError {
7878
/// An amount was expected but was missing.
7979
MissingAmount,
80+
/// An amount exceeded the maximum number of bitcoin.
81+
InvalidAmount,
8082
/// A required description was not provided.
8183
MissingDescription,
8284
/// A node id was not provided.

0 commit comments

Comments
 (0)