2 files changed
+35
-22
crates/smbcloud-model/src/account.rs
+1
@@ -48,6 +48,7 @@ pub struct SmbAuthorization {
48
pub error_code: Option<ErrorCode>,
49
}
50
51
+/// TODO: Move to ErrorResponse
52
#[derive(Debug, Serialize_repr, Deserialize_repr, PartialEq)]
53
#[repr(u32)]
54
#[tsync]
crates/smbcloud-network/src/network.rs
+34
-22
@@ -52,7 +52,7 @@ pub async fn check_internet_connection() -> bool {
52
pub async fn parse_error_response<T: DeserializeOwned>(
53
response: Response,
54
) -> Result<T, ErrorResponse> {
55
- let response_body = match response.text().await {
55
+ let error_response_body = match response.text().await {
56
Ok(body) => body,
57
Err(e) => {
58
error!("Failed to get response body: {:?}", e);
@@ -66,13 +66,13 @@ pub async fn parse_error_response<T: DeserializeOwned>(
66
if LOG_RESPONSE_BODY {
67
println!();
68
println!("Parse Error >>>>");
69
- println!("{:?}", serde_json::to_string_pretty(&response_body));
69
+ println!("{:?}", serde_json::to_string_pretty(&error_response_body));
70
println!("Parse Error >>>>");
71
println!();
72
}
73
74
- let e = match serde_json::from_str::<ErrorResponse>(&response_body) {
75
- Ok(json) => json,
74
+ let error_response = match serde_json::from_str::<T>(&error_response_body) {
75
+ Ok(error_response) => error_response,
76
Err(e) => {
77
error!("Failed to parse error response: {:?}", e);
78
return Err(ErrorResponse::Error {
@@ -81,8 +81,8 @@ pub async fn parse_error_response<T: DeserializeOwned>(
81
});
82
}
83
};
84
- error!("Error response: {:?}", e);
85
- Err(e)
84
+ // The parsing itself is succeed.
85
+ Ok(error_response)
86
}
87
88
pub async fn request_login(builder: RequestBuilder) -> Result<AccountStatus, ErrorResponse> {
@@ -90,7 +90,7 @@ pub async fn request_login(builder: RequestBuilder) -> Result<AccountStatus, Err
90
let response = match response {
91
Ok(response) => response,
92
Err(e) => {
93
- error!("Failed to get response: {:?}", e);
93
+ error!("request_login: Failed to get response: {:?}", e);
94
return Err(ErrorResponse::Error {
95
error_code: ErrorCode::NetworkError,
96
message: ErrorCode::NetworkError.message(None).to_string(),
@@ -98,24 +98,11 @@ pub async fn request_login(builder: RequestBuilder) -> Result<AccountStatus, Err
98
}
99
};
100
101
- let response = match response.status() {
102
- reqwest::StatusCode::OK
103
- | reqwest::StatusCode::NOT_FOUND
104
- | reqwest::StatusCode::UNPROCESSABLE_ENTITY => response,
105
- status => {
106
- error!(
107
- "Response are neither OK, NOT_FOUND, or UNPROCESSABLE_ENTITY: {:?}",
108
- status
109
- );
110
- return parse_error_response(response).await;
111
- }
112
- };
113
-
101
if LOG_RESPONSE_BODY {
102
println!();
116
- println!("Parse >>>>");
103
+ println!("request_login: Parse >>>>");
104
println!("{:?}", &response.status());
118
- println!("Parse >>>>");
105
+ println!("request_login: Parse >>>>");
106
println!();
107
}
108
@@ -133,6 +120,30 @@ pub async fn request_login(builder: RequestBuilder) -> Result<AccountStatus, Err
120
};
121
Ok(AccountStatus::Ready { access_token })
122
}
123
+ (StatusCode::OK, None) => {
124
+ // Silent login from oauth. Need improvement.
125
+ let error_response = match parse_error_response::<ErrorResponse>(response).await {
126
+ Ok(error) => error,
127
+ Err(_) => return Ok(AccountStatus::NotFound),
128
+ };
129
+ match error_response {
130
+ ErrorResponse::Error {
131
+ error_code,
132
+ message,
133
+ } => match error_code {
134
+ ErrorCode::EmailNotVerified => Ok(AccountStatus::Incomplete {
135
+ status: smbcloud_model::account::ErrorCode::EmailUnverified,
136
+ }),
137
+ ErrorCode::PasswordNotSet => Ok(AccountStatus::Incomplete {
138
+ status: smbcloud_model::account::ErrorCode::PasswordNotSet,
139
+ }),
140
+ ErrorCode::Unknown => Ok(AccountStatus::Ready {
141
+ access_token: "tokenization".to_string(),
142
+ }),
143
+ _ => Ok(AccountStatus::NotFound),
144
+ },
145
+ }
146
+ }
147
(StatusCode::NOT_FOUND, _) => {
148
// Account not found
149
Ok(AccountStatus::NotFound)
@@ -193,6 +204,7 @@ pub async fn request<R: DeserializeOwned>(builder: RequestBuilder) -> Result<R,
204
reqwest::StatusCode::OK | reqwest::StatusCode::CREATED => response,
205
status => {
206
error!("Failed to get response: {:?}", status);
207
+ // This should handle parsing the error response.
208
return parse_error_response(response).await;
209
}
210
};