I doing a learning project with actix and a local json database so this whole project may seem a bit strange but my problem is that I'm getting this error message:
thread 'actix-rt:worker:0' panicked at 'what the heck: Os { code: 9, kind: Other, message: "Bad file descriptor" }'
I don't understand why.
I have an endpoint "localhost.../signup" that is calling "post_signup" handler function.
The panic occurs in the function "update_user_index" when I'm trying to write to the file and I just don't get it.
File exists, filename and path is correct.
If I missed any relevant info, let me know.
pub async fn post_signup(body: web::Json<User>) -> Result<HttpResponse> {
match get_user_by_email(body.clone().email) {
Some(body) => Ok(HttpResponse::Unauthorized().json(LoginResponse {
name: body.email.to_string(),
message: format!("User '{}' already exists, sign in instead.", body.email.to_string()),
})),
None => {
let new_user = add_new_user(&body)?;
Ok(HttpResponse::Ok()
.cookie(
http::Cookie::build("jwt", "j903jfdsfff")
.domain("")
.path("/")
.secure(false)
.http_only(true)
.max_age(60 * 60 * 24 * 7 * 4 * 3)
.finish(),
)
.json(LoginResponse {
name: new_user.name.to_string(),
message: format!("{}, logged in.", new_user.name.to_string()),
}))
}
}
}
pub fn get_user_index() -> Result<UserIndex> {
let mut file = File::open("./database/index_users.json")?;
let mut data = String::new();
file.read_to_string(&mut data)?;
Ok(serde_json::from_str(&data)?)
}
pub fn update_user_index(new_user: Index) -> Result<()> {
let mut users: UserIndex = get_user_index()?;
users.push(new_user);
println!("{:?}", &users);
let mut file = File::open("./database/index_users.json")?;
write!(file, "{}", serde_json::to_string(&users)?).expect("what the heck");
Ok(())
}
pub fn get_user_file(id: String) -> Result<User> {
let mut file = File::open(format!("./database/users/{}.json", id))?;
let mut data = String::new();
file.read_to_string(&mut data)?;
Ok(serde_json::from_str(&data)?)
}