Skip to content

Commit 4f743a4

Browse files
committed
Use SemanticError in OfferBuilder::build
1 parent 7c5ed02 commit 4f743a4

File tree

2 files changed

+7
-5
lines changed

2 files changed

+7
-5
lines changed

lightning/src/offers/offer.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,7 @@
4848
//! .issuer("Foo Bar".to_string())
4949
//! .path(create_blinded_path())
5050
//! .path(create_another_blinded_path())
51-
//! .build()
52-
//! .unwrap();
51+
//! .build()?;
5352
//!
5453
//! // Encode as a bech32 string for use in a QR code.
5554
//! let encoded_offer = offer.to_string();
@@ -189,11 +188,11 @@ impl OfferBuilder {
189188
}
190189

191190
/// Builds an [`Offer`] from the builder's settings.
192-
pub fn build(self) -> Result<Offer, ()> {
191+
pub fn build(self) -> Result<Offer, SemanticError> {
193192
// TODO: Also check for Amount::Currency
194193
if let Some(Amount::Currency { .. }) = self.offer.amount {
195194
} else if self.offer.amount_msats() > MAX_VALUE_MSAT {
196-
return Err(());
195+
return Err(SemanticError::InvalidAmount);
197196
}
198197

199198
let mut bytes = Vec::new();
@@ -550,6 +549,7 @@ mod tests {
550549
use core::time::Duration;
551550
use crate::ln::features::OfferFeatures;
552551
use crate::ln::msgs::MAX_VALUE_MSAT;
552+
use crate::offers::parse::SemanticError;
553553
use crate::onion_message::{BlindedHop, BlindedPath};
554554
use crate::util::ser::Writeable;
555555
use crate::util::string::PrintableString;
@@ -686,7 +686,7 @@ mod tests {
686686
let invalid_amount = Amount::Bitcoin { amount_msats: MAX_VALUE_MSAT + 1 };
687687
match OfferBuilder::new("foo".into(), pubkey(42)).amount(invalid_amount).build() {
688688
Ok(_) => panic!("expected error"),
689-
Err(e) => assert_eq!(e, ()),
689+
Err(e) => assert_eq!(e, SemanticError::InvalidAmount),
690690
}
691691
}
692692

lightning/src/offers/parse.rs

Lines changed: 2 additions & 0 deletions
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)