Return unparseable country code as error instead of silent failure#74
Open
Return unparseable country code as error instead of silent failure#74
Conversation
Overrides the behaviour of #54 Co-authored-by: Regis David Souza Mesquita <regis@telnyx.com> Co-authored-by: Raphael Costa <vidal.raphael@gmail.com>
Member
Author
diff --git a/src/error.rs b/src/error.rs
index c7799df..0c4fa1a 100644
--- a/src/error.rs
+++ b/src/error.rs
@@ -60,7 +60,7 @@ pub enum Parse {
/// non-geographical entity.
#[error("invalid country code")]
#[allow(unused)] // This is unused in the build script
- InvalidCountryCode,
+ InvalidCountryCode(String),
/// This indicates the string started with an international dialing prefix,
/// but after this was stripped from the number, had less digits than any
diff --git a/src/parser/helper.rs b/src/parser/helper.rs
index 36f73a4..acceb05 100644
--- a/src/parser/helper.rs
+++ b/src/parser/helper.rs
@@ -160,7 +160,7 @@ pub fn country_code<'a>(
let prefix = number.prefix.as_ref().unwrap().parse()?;
if database.by_code(&prefix).is_none() {
- return Err(error::Parse::InvalidCountryCode);
+ return Err(error::Parse::InvalidCountryCode(prefix.to_string()));
} else {
return Ok(number);
}
diff --git a/src/phone_number.rs b/src/phone_number.rs
index 8907324..94dc08d 100644
--- a/src/phone_number.rs
+++ b/src/phone_number.rs
@@ -237,11 +237,17 @@ impl<'a> Country<'a> {
}
pub fn id(&self) -> Result<Option<country::Id>, crate::error::Parse> {
- self.0
- .metadata(&DATABASE)
- .map(|m| m.id().parse())
- .transpose()
- .map_err(|_e| crate::error::Parse::InvalidCountryCode)
+ let metadata = if let Some(metadata) = self.0.metadata(&DATABASE) {
+ metadata
+ } else {
+ return Ok(None);
+ };
+ match metadata.id().parse() {
+ Ok(id) => Ok(Some(id)),
+ Err(e) => Err(crate::error::Parse::InvalidCountryCode(
+ metadat.id().to_string(),
+ )),
+ }
}
}We'd need a separate error type, because many other points in the code trigger that error without any available context. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Overrides the behaviour of #54
Cc @regismesquita @costaraphael, do you think this is an appropriate error, or should we propagate the unparsable
id()call too?Fixes #56