Skip to content

Commit 50bad4e

Browse files
committed
refactor: pass the full app state to the error response generation function
this will allow creating more user-friendly errors
1 parent d14c38c commit 50bad4e

File tree

1 file changed

+13
-12
lines changed

1 file changed

+13
-12
lines changed

src/webserver/http.rs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use crate::webserver::database::execute_queries::stop_at_first_error;
88
use crate::webserver::database::{execute_queries::stream_query_results_with_conn, DbItem};
99
use crate::webserver::http_request_info::extract_request_info;
1010
use crate::webserver::ErrorWithStatus;
11-
use crate::{app_config, AppConfig, AppState, ParsedSqlFile, DEFAULT_404_FILE};
11+
use crate::{AppConfig, AppState, ParsedSqlFile, DEFAULT_404_FILE};
1212
use actix_web::dev::{fn_service, ServiceFactory, ServiceRequest};
1313
use actix_web::error::{ErrorBadRequest, ErrorInternalServerError};
1414
use actix_web::http::header::{ContentType, Header, HttpDate, IfModifiedSince, LastModified};
@@ -170,7 +170,7 @@ async fn render_sql(
170170

171171
let mut req_param = extract_request_info(srv_req, Arc::clone(&app_state))
172172
.await
173-
.map_err(|e| anyhow_err_to_actix(e, app_state.config.environment))?;
173+
.map_err(|e| anyhow_err_to_actix(e, &app_state))?;
174174
log::debug!("Received a request with the following parameters: {req_param:?}");
175175

176176
let (resp_send, resp_recv) = tokio::sync::oneshot::channel::<HttpResponse>();
@@ -206,16 +206,17 @@ async fn render_sql(
206206
.unwrap_or_else(|e| log::error!("could not send headers {e:?}"));
207207
}
208208
Err(err) => {
209-
send_anyhow_error(&err, resp_send, app_state.config.environment);
209+
send_anyhow_error(&err, resp_send, &app_state);
210210
}
211211
}
212212
});
213213
resp_recv.await.map_err(ErrorInternalServerError)
214214
}
215215

216-
fn anyhow_err_to_actix_resp(e: &anyhow::Error, env: app_config::DevOrProd) -> HttpResponse {
216+
fn anyhow_err_to_actix_resp(e: &anyhow::Error, state: &AppState) -> HttpResponse {
217217
let mut resp = HttpResponseBuilder::new(StatusCode::INTERNAL_SERVER_ERROR);
218218
let mut body = "Sorry, but we were not able to process your request.\n\n".to_owned();
219+
let env = state.config.environment;
219220
if env.is_prod() {
220221
body.push_str("Contact the administrator for more information. A detailed error message has been logged.");
221222
log::error!("{e:#}");
@@ -254,17 +255,17 @@ fn anyhow_err_to_actix_resp(e: &anyhow::Error, env: app_config::DevOrProd) -> Ht
254255
fn send_anyhow_error(
255256
e: &anyhow::Error,
256257
resp_send: tokio::sync::oneshot::Sender<HttpResponse>,
257-
env: app_config::DevOrProd,
258+
state: &AppState,
258259
) {
259260
log::error!("An error occurred before starting to send the response body: {e:#}");
260261
resp_send
261-
.send(anyhow_err_to_actix_resp(e, env))
262+
.send(anyhow_err_to_actix_resp(e, state))
262263
.unwrap_or_else(|_| log::error!("could not send headers"));
263264
}
264265

265-
fn anyhow_err_to_actix(e: anyhow::Error, env: app_config::DevOrProd) -> actix_web::Error {
266+
fn anyhow_err_to_actix(e: anyhow::Error, state: &AppState) -> actix_web::Error {
266267
log::error!("{e:#}");
267-
let resp = anyhow_err_to_actix_resp(&e, env);
268+
let resp = anyhow_err_to_actix_resp(&e, state);
268269
actix_web::error::InternalError::from_response(e, resp).into()
269270
}
270271

@@ -331,7 +332,7 @@ async fn process_sql_request(
331332
.get_with_privilege(app_state, &sql_path, false)
332333
.await
333334
.with_context(|| format!("Unable to get SQL file \"{}\"", sql_path.display()))
334-
.map_err(|e| anyhow_err_to_actix(e, app_state.config.environment))?;
335+
.map_err(|e| anyhow_err_to_actix(e, app_state))?;
335336
render_sql(req, sql_file).await
336337
}
337338

@@ -348,7 +349,7 @@ async fn serve_file(
348349
.modified_since(state, path.as_ref(), since, false)
349350
.await
350351
.with_context(|| format!("Unable to get modification time of file {path:?}"))
351-
.map_err(|e| anyhow_err_to_actix(e, state.config.environment))?;
352+
.map_err(|e| anyhow_err_to_actix(e, state))?;
352353
if !modified {
353354
return Ok(HttpResponse::NotModified().finish());
354355
}
@@ -358,7 +359,7 @@ async fn serve_file(
358359
.read_file(state, path.as_ref(), false)
359360
.await
360361
.with_context(|| format!("Unable to read file {path:?}"))
361-
.map_err(|e| anyhow_err_to_actix(e, state.config.environment))
362+
.map_err(|e| anyhow_err_to_actix(e, state))
362363
.map(|b| {
363364
HttpResponse::Ok()
364365
.insert_header(
@@ -391,7 +392,7 @@ pub async fn main_handler(
391392
let e = e.context(format!(
392393
"Unable to calculate the routing action for: {path_and_query:?}"
393394
));
394-
return Err(anyhow_err_to_actix(e, app_state.config.environment));
395+
return Err(anyhow_err_to_actix(e, app_state));
395396
}
396397
};
397398
match routing_action {

0 commit comments

Comments
 (0)