Skip to content

Commit 0d382a1

Browse files
Copilotazerpas
andcommitted
Make error handling consistent across all extraction functions
Co-authored-by: azerpas <19282069+azerpas@users.noreply.github.com>
1 parent c2abb7a commit 0d382a1

1 file changed

Lines changed: 14 additions & 16 deletions

File tree

  • src/bourso_api/src/client

src/bourso_api/src/client/mod.rs

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -567,22 +567,20 @@ fn extract_token(res: &str) -> Result<String> {
567567
/// # Returns
568568
/// A tuple containing the resource ID and form state as strings.
569569
fn extract_otp_params(res: &str) -> Result<(String, String)> {
570-
let captures = OTP_PARAMS_REGEX.captures(&res);
571-
572-
let challenge_json = if let Some(captures) = captures {
573-
let challenge_str = captures.get(1)
574-
.map(|m| m.as_str())
575-
.ok_or_else(|| {
576-
error!("{}", res);
577-
anyhow::anyhow!("Could not extract authentication challenge from regex match")
578-
})?;
579-
// HTML decode the JSON string (replace &quot; with ")
580-
let decoded = challenge_str.replace("&quot;", "\"");
581-
serde_json::from_str::<serde_json::Value>(&decoded)?
582-
} else {
583-
error!("{}", res);
584-
bail!("Could not extract authentication challenge parameters");
585-
};
570+
let challenge_json = OTP_PARAMS_REGEX
571+
.captures(&res)
572+
.and_then(|c| c.get(1))
573+
.map(|m| m.as_str())
574+
.ok_or_else(|| {
575+
error!("{}", res);
576+
anyhow::anyhow!("Could not extract authentication challenge parameters")
577+
})
578+
.and_then(|challenge_str| {
579+
// HTML decode the JSON string (replace &quot; with ")
580+
let decoded = challenge_str.replace("&quot;", "\"");
581+
serde_json::from_str::<serde_json::Value>(&decoded)
582+
.map_err(|e| anyhow::anyhow!("Could not parse authentication challenge JSON: {}", e))
583+
})?;
586584

587585
Ok((
588586
challenge_json["challenges"][0]["parameters"]["formScreen"]["actions"]["check"]["api"]

0 commit comments

Comments
 (0)