Skip to content

Commit

Permalink
Change error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Hinton committed Jul 17, 2023
1 parent ec2d645 commit ff67d65
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
10 changes: 5 additions & 5 deletions crates/bitwarden/src/auth/api/request/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@ async fn send_identity_connect_request(
request = request.header("Auth-Email", BASE64_ENGINE.encode(email.as_bytes()));
}

let raw_response = request
let response = request
.body(serde_qs::to_string(&body).unwrap())
.send()
.await?
.error_for_status()?
.text()
.await?;

parse_identity_response(&raw_response)
let status = response.status();
let text = response.text().await?;

parse_identity_response(status, &text)
}
17 changes: 12 additions & 5 deletions crates/bitwarden/src/auth/api/response/identity_token_response.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use reqwest::StatusCode;
use serde::{Deserialize, Serialize};

use crate::{
Expand All @@ -17,7 +18,10 @@ pub enum IdentityTokenResponse {
CaptchaRequired(IdentityCaptchaResponse),
}

pub fn parse_identity_response(response: &str) -> Result<IdentityTokenResponse> {
pub fn parse_identity_response(
status: StatusCode,
response: &str,
) -> Result<IdentityTokenResponse> {
if let Ok(r) = serde_json::from_str::<IdentityTokenSuccessResponse>(response) {
Ok(IdentityTokenResponse::Authenticated(r))
} else if let Ok(r) = serde_json::from_str::<IdentityTokenPayloadResponse>(response) {
Expand All @@ -31,7 +35,10 @@ pub fn parse_identity_response(response: &str) -> Result<IdentityTokenResponse>
} else if let Ok(r) = serde_json::from_str::<IdentityTokenFailResponse>(response) {
Err(Error::IdentityFail(r))
} else {
Err(Error::Internal("Failed to parse IdentityTokenResponse"))
Err(Error::ResponseContent {
status: status,
message: response.to_owned(),
})
}
}

Expand All @@ -44,7 +51,7 @@ mod test {
let expected = IdentityTokenSuccessResponse::default();
let success = serde_json::to_string(&expected).unwrap();
let expected = IdentityTokenResponse::Authenticated(expected);
let actual = parse_identity_response(&success).unwrap();
let actual = parse_identity_response(StatusCode::OK, &success).unwrap();
assert_eq!(expected, actual);
}

Expand All @@ -53,7 +60,7 @@ mod test {
let expected = IdentityTwoFactorResponse::default();
let two_factor = serde_json::to_string(&expected).unwrap();
let expected = IdentityTokenResponse::TwoFactorRequired(expected);
let actual = parse_identity_response(&two_factor).unwrap();
let actual = parse_identity_response(StatusCode::BAD_REQUEST, &two_factor).unwrap();
assert_eq!(expected, actual);
}

Expand All @@ -62,7 +69,7 @@ mod test {
let expected = IdentityCaptchaResponse::default();
let captcha = serde_json::to_string(&expected).unwrap();
let expected = IdentityTokenResponse::CaptchaRequired(expected);
let actual = parse_identity_response(&captcha).unwrap();
let actual = parse_identity_response(StatusCode::BAD_REQUEST, &captcha).unwrap();
assert_eq!(expected, actual);
}
}

0 comments on commit ff67d65

Please sign in to comment.