I got the following runtime-error:
thread 'actix-rt|system:0|arbiter:1' panicked at 'called `Result::unwrap()` on an `Err` value: PoisonError { .. }', src/route_handlers/api.rs:238:55
At this piece of code:
#[derive(Deserialize, Serialize)]
pub struct HellofoodConfigResponse {
transaction_fee: f64,
}
pub async fn get_hellofood_config(
hellofood_config: web::Data<Mutex<HellofoodConfig>>,
) -> Result<HttpResponse> {
let my_hellofood_config = hellofood_config.lock().unwrap();
Ok(HttpResponse::Ok().content_type("application/json").body(
serde_json::to_string(&HellofoodConfigResponse {
transaction_fee: my_hellofood_config
.config
.get("transaction_fee")
.unwrap()
.to_string()
.parse::<f64>()
.unwrap(),
})
.unwrap(),
))
}
I think it has something to do with my_hellofood_config
.
It is the following struct
// Wrapper for Config
#[derive(Clone)]
pub struct HellofoodConfig {
pub config: std::collections::HashMap<String, String>,
}
impl HellofoodConfig {
pub async fn get_config(mysql: &web::Data<MySQL>) -> std::collections::HashMap<String, String> {
let mut result = sqlx::query("SELECT config, value FROM config").fetch(&mysql.conn);
let mut config = std::collections::HashMap::new();
while let Some(row) = result.try_next().await.unwrap() {
config.insert(
row.try_get("config").unwrap(),
row.try_get("value").unwrap(),
);
}
config
}
pub async fn update(config: String, value: String, mysql: &web::Data<MySQL>) -> bool {
let result = sqlx::query("UPDATE config SET value=? WHERE config=?")
.bind(&value)
.bind(&config)
.execute(&mysql.conn)
.await;
match result {
Err(e) => {
println!(
"Error [in update() of struct HellofoodConfig, main.rs]: {}",
e
);
false
}
Ok(r) => r.rows_affected() > 0,
}
}
}
Passed as app_data
in Actix web like this
let hellofood_config = web::Data::new(Mutex::new(HellofoodConfig {
config: HellofoodConfig::get_config(&mysql).await,
}));
//...
.app_data(web::Data::clone(&hellofood_config))
//...
I have a global configuration (like transaction-fees, commission-fees,...) for my platform which needs to be dynamic so I can change them at runtime. These configurations are saved in the database by the HellofoodConfig object.
I never had this error before and recompiling the application solved it. How can I prevent this error in the future though?