Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return unparseable country code as error instead of silent failure #74

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

rubdos
Copy link
Member

@rubdos rubdos commented Jul 7, 2024

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

Overrides the behaviour of #54

Co-authored-by: Regis David Souza Mesquita <[email protected]>
Co-authored-by: Raphael Costa <[email protected]>
@rubdos
Copy link
Member Author

rubdos commented Jul 7, 2024

[..] should we propagate the unparsable id() call too?

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.

@rubdos rubdos marked this pull request as draft July 9, 2024 09:38
@rubdos rubdos added this to the 0.4 milestone Jul 9, 2024
@rubdos rubdos requested a review from gferon July 9, 2024 09:40
@rubdos rubdos marked this pull request as ready for review July 9, 2024 09:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Propagate error through Result from Country::id(&self)
1 participant