Simple bug fix

This commit is contained in:
2025-09-27 10:26:13 +02:00
parent 1aa73441c7
commit a265c19870
3 changed files with 39 additions and 5 deletions
+6 -1
View File
@@ -2,8 +2,11 @@ use std::sync::Arc;
use serde::Deserialize; use serde::Deserialize;
use crate::{ErrorContext, logger};
use crate::{Server, utils::client::Client}; use crate::{Server, utils::client::Client};
logger!(LOGGER "Auth");
#[derive(Debug, Deserialize)] #[derive(Debug, Deserialize)]
struct AuthApiRes { struct AuthApiRes {
user_id: u32, user_id: u32,
@@ -13,8 +16,10 @@ pub fn auth(_server: &Arc<Server>, client: &mut Client, token: &str) -> crate::R
let mut res = ureq::get(format!( let mut res = ureq::get(format!(
"http://localhost:3000/api/auth?intents=server&token={token}" "http://localhost:3000/api/auth?intents=server&token={token}"
)) ))
.call()?; .call()
.context("Failed to authenticate")?;
let api_res: AuthApiRes = serde_json::from_str(&res.body_mut().read_to_string()?)?; let api_res: AuthApiRes = serde_json::from_str(&res.body_mut().read_to_string()?)?;
client.set_uuid(api_res.user_id); client.set_uuid(api_res.user_id);
LOGGER.info(format!("{} successfully authenticated", api_res.user_id));
Ok(api_res.user_id) Ok(api_res.user_id)
} }
+13 -4
View File
@@ -11,6 +11,7 @@ pub mod requests;
pub mod types; pub mod types;
pub mod utils; pub mod utils;
pub use anyhow::Context as ErrorContext;
pub use anyhow::Result; pub use anyhow::Result;
use crate::{utils::client::Client, utils::plugin::DynPlugin}; use crate::{utils::client::Client, utils::plugin::DynPlugin};
@@ -89,10 +90,18 @@ impl Server {
Ok(stream) => { Ok(stream) => {
std::thread::spawn({ std::thread::spawn({
let srv = self.clone(); let srv = self.clone();
let client = srv.init_client(stream)?;
move || match srv.wrap_err(&client, srv.handle_client(&client)) { move || {
Ok(_) => {} let Some(client) = Self::LOGGER
Err(e) => Self::LOGGER.error(format!("Client handler failed: {e}")), .extract(srv.init_client(stream), "Failed to initialize client")
else {
return;
};
Self::LOGGER.extract(
srv.wrap_err(&client, srv.handle_client(&client)),
"Client handler failed",
);
} }
}); });
} }
+20
View File
@@ -22,4 +22,24 @@ impl Logger {
pub fn error<T: Display>(&self, message: T) { pub fn error<T: Display>(&self, message: T) {
println!("\x1b[31mERROR\x1b[0m ({}) › {}", self.name, message); println!("\x1b[31mERROR\x1b[0m ({}) › {}", self.name, message);
} }
pub fn extract<T, E: Display, D: Display>(&self, v: Result<T, E>, m: D) -> Option<T> {
match v {
Ok(a) => Some(a),
Err(e) => {
self.error(format!("{m}: {e}"));
None
}
}
}
pub fn extract_panic<T, E: Display, D: Display>(&self, v: Result<T, E>, m: D) -> T {
match v {
Ok(a) => a,
Err(e) => {
self.error(format!("{m}: {e}"));
panic!()
}
}
}
} }