Skip to content

Commit a032063

Browse files
committed
Improve logging for verifier middleware errors
1 parent 692869f commit a032063

File tree

1 file changed

+81
-27
lines changed

1 file changed

+81
-27
lines changed

fplus-http-server/src/middleware/verifier_auth.rs

Lines changed: 81 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ where
5252

5353
fn call(&self, req: ServiceRequest) -> Self::Future {
5454
let query_string = req.query_string();
55+
let method = req.method();
56+
let path = req.path();
57+
let request_info = format!("{method} {path}?{query_string}");
5558
let query: Result<web::Query<RepoQuery>, _> = web::Query::from_query(query_string);
5659
let RepoQuery {
5760
owner,
@@ -60,12 +63,15 @@ where
6063
} = match query {
6164
Ok(q) => q.into_inner(),
6265
Err(e) => {
63-
println!("{e}");
64-
return Box::pin(async {
65-
Err(actix_web::error::ErrorBadRequest(
66-
"Wrong query string format",
67-
))
68-
});
66+
let err = actix_web::error::ErrorBadRequest(format!(
67+
"Wrong query string format error: {e}"
68+
));
69+
log::info!(
70+
"{} {}",
71+
request_info,
72+
err.as_response_error().status_code().as_u16()
73+
);
74+
return Box::pin(async { Err(err) });
6975
}
7076
};
7177

@@ -89,36 +95,71 @@ where
8995
.header("User-Agent", "Actix-web")
9096
.send()
9197
.await
92-
.map_err(actix_web::error::ErrorBadRequest)?;
98+
.map_err(|e| {
99+
let err = actix_web::error::ErrorBadRequest(e);
100+
log::info!(
101+
"{} {}",
102+
request_info,
103+
err.as_response_error().status_code().as_u16()
104+
);
105+
err
106+
})?;
93107

94108
if user_info_result.status().is_success() {
95-
let user_info = user_info_result
96-
.json::<serde_json::Value>()
97-
.await
98-
.expect("Failed to parse JSON");
109+
let user_info =
110+
user_info_result
111+
.json::<serde_json::Value>()
112+
.await
113+
.map_err(|_| {
114+
let err = actix_web::error::ErrorInternalServerError(
115+
"GitHub handle information not found.",
116+
);
117+
log::info!(
118+
"{} {}",
119+
request_info,
120+
err.as_response_error().status_code().as_u16()
121+
);
122+
log::error!("{err}");
123+
err
124+
})?;
99125

100126
if let Some(login) = user_info.get("login").and_then(|v| v.as_str()) {
101127
user_handle = login.to_string();
102128
} else {
103-
println!("GitHub handle information not found.");
104-
return Err(actix_web::error::ErrorInternalServerError(
129+
let err = actix_web::error::ErrorInternalServerError(
105130
"GitHub handle information not found.",
106-
));
131+
);
132+
log::info!(
133+
"{} {}",
134+
request_info,
135+
err.as_response_error().status_code().as_u16()
136+
);
137+
log::error!("{err}");
138+
return Err(err);
107139
}
108140
} else {
109-
println!("Failed to get GitHub user info");
110-
return Err(actix_web::error::ErrorUnauthorized(
111-
"Failed to get GitHub user info.",
112-
));
141+
let err = actix_web::error::ErrorUnauthorized("Failed to get GitHub user info");
142+
log::info!(
143+
"{} {}",
144+
request_info,
145+
err.as_response_error().status_code().as_u16()
146+
);
147+
log::error!("{err}");
148+
return Err(err);
113149
}
114150
}
115151

116152
if github_username != user_handle {
117-
// comment this for testing
118-
println!("Sent GitHub handle different than auth token owner.");
119-
return Err(actix_web::error::ErrorBadRequest(
153+
let err = actix_web::error::ErrorUnauthorized(
120154
"Sent GitHub handle different than auth token owner.",
121-
));
155+
);
156+
log::info!(
157+
"{} {}",
158+
request_info,
159+
err.as_response_error().status_code().as_u16()
160+
);
161+
log::error!("{err}");
162+
return Err(err);
122163
}
123164

124165
match get_allocator(&owner, &repo).await {
@@ -130,18 +171,31 @@ where
130171
.map(|s| s.trim().to_lowercase())
131172
.collect();
132173
if verifier_handles.contains(&user_handle.to_lowercase()) {
133-
println!("{user_handle} is a verifier.");
174+
log::info!("{user_handle} is a verifier.");
134175
} else {
135-
println!("The user is not a verifier.");
136-
return Err(actix_web::error::ErrorUnauthorized(
176+
let err = actix_web::error::ErrorUnauthorized(
137177
"The user is not a verifier.",
138-
));
178+
);
179+
log::info!(
180+
"{} {}",
181+
request_info,
182+
err.as_response_error().status_code().as_u16()
183+
);
184+
log::error!("{err}");
185+
return Err(err);
139186
}
140187
}
141188
}
142189
}
143190
Err(e) => {
144-
println!("Failed to get allocator: {e:?}");
191+
let err = actix_web::error::ErrorInternalServerError(e);
192+
log::info!(
193+
"{} {}",
194+
request_info,
195+
err.as_response_error().status_code().as_u16()
196+
);
197+
log::error!("{err}");
198+
return Err(err);
145199
}
146200
}
147201

0 commit comments

Comments
 (0)