diff --git a/.env b/.env index 60ff916..70e3dfb 100644 --- a/.env +++ b/.env @@ -1,4 +1,5 @@ DB_HOST=u_db DB_NAME=u_db DB_USER=postgres -RUST_BACKTRACE=1 \ No newline at end of file +RUST_BACKTRACE=1 +U_SERVER=u_server \ No newline at end of file diff --git a/Makefile.toml b/Makefile.toml index 32533ec..e70a9dc 100644 --- a/Makefile.toml +++ b/Makefile.toml @@ -4,11 +4,12 @@ default_to_workspace = false [env] TARGET = "x86_64-unknown-linux-musl" CARGO = "cargo" -PREFIX = "${CARGO_MAKE_WORKING_DIRECTORY}/static" +ROOTDIR = "${CARGO_MAKE_WORKING_DIRECTORY}" +STATIC_PREFIX = "${ROOTDIR}/static" PQ_LIB_STATIC_X86_64_UNKNOWN_LINUX_MUSL = "true" -PG_CONFIG_X86_64_UNKNOWN_LINUX_GNU = "${PREFIX}/bin/pg_config" +PG_CONFIG_X86_64_UNKNOWN_LINUX_GNU = "${STATIC_PREFIX}/bin/pg_config" OPENSSL_STATIC = "true" -OPENSSL_DIR = "${PREFIX}" +OPENSSL_DIR = "${STATIC_PREFIX}" [tasks.build_static_libs] script = "./scripts/build_musl_libs.sh" @@ -32,7 +33,7 @@ args = ["build", "--target", "${TARGET}", "${@}"] script = ''' if [[ "${@}" =~ "--release" ]]; then echo "Creating symlink to release dir..." - ln -s ./target/${TARGET}/release ./release || true + ln -s ${ROOTDIR}/target/${TARGET}/release ${ROOTDIR}/release || true BINS=$(ls ./release/u_* -1 | grep -v ".d") echo "Stripping..." strip $BINS diff --git a/bin/u_panel/src/argparse.rs b/bin/u_panel/src/argparse.rs index cfe9050..c9f8c0f 100644 --- a/bin/u_panel/src/argparse.rs +++ b/bin/u_panel/src/argparse.rs @@ -1,7 +1,7 @@ use std::fmt; use structopt::StructOpt; use u_lib::{ - api::ClientHandler, datatypes::DataResult, messaging::AsMsg, models::JobMeta, UError, UResult, + api::ClientHandler, datatypes::PanelResult, messaging::AsMsg, models::JobMeta, UError, UResult, }; use uuid::Uuid; @@ -92,8 +92,8 @@ pub async fn process_cmd(client: ClientHandler, args: Args) -> UResult<()> { pub fn print(&self, data: UResult) { if self.json { let data = match data { - Ok(r) => DataResult::Ok(r), - Err(e) => DataResult::Err(e), + Ok(r) => PanelResult::Ok(r), + Err(e) => PanelResult::Err(e), }; println!("{}", serde_json::to_string_pretty(&data).unwrap()); } else { diff --git a/bin/u_server/src/db.rs b/bin/u_server/src/db.rs index cff590f..db064ec 100644 --- a/bin/u_server/src/db.rs +++ b/bin/u_server/src/db.rs @@ -1,3 +1,4 @@ +use crate::errors::{Error, SResult}; use diesel::{pg::PgConnection, prelude::*, result::Error as DslError}; use once_cell::sync::OnceCell; use serde::Deserialize; @@ -5,7 +6,6 @@ use std::sync::{Arc, Mutex, MutexGuard}; use u_lib::{ models::{schema, Agent, AgentError, AssignedJob, JobMeta, JobState}, utils::load_env, - ULocalError, ULocalResult, }; use uuid::Uuid; @@ -39,7 +39,7 @@ impl UDB { .unwrap() } - pub fn report_error(&self, error: &AgentError) -> ULocalResult<()> { + pub fn report_error(&self, error: &AgentError) -> SResult<()> { use schema::errors; diesel::insert_into(errors::table) .values(error) @@ -47,7 +47,7 @@ impl UDB { Ok(()) } - pub fn insert_jobs(&self, job_metas: &[JobMeta]) -> ULocalResult<()> { + pub fn insert_jobs(&self, job_metas: &[JobMeta]) -> SResult<()> { use schema::jobs; diesel::insert_into(jobs::table) .values(job_metas) @@ -55,7 +55,7 @@ impl UDB { Ok(()) } - pub fn get_jobs(&self, uid: Option) -> ULocalResult> { + pub fn get_jobs(&self, uid: Option) -> SResult> { use schema::jobs; let result = if uid.is_some() { jobs::table @@ -67,7 +67,7 @@ impl UDB { Ok(result) } - pub fn find_job_by_alias(&self, alias: &str) -> ULocalResult { + pub fn find_job_by_alias(&self, alias: &str) -> SResult { use schema::jobs; let result = jobs::table .filter(jobs::alias.eq(alias)) @@ -75,7 +75,7 @@ impl UDB { Ok(result) } - pub fn insert_agent(&self, agent: &Agent) -> ULocalResult<()> { + pub fn insert_agent(&self, agent: &Agent) -> SResult<()> { use schema::agents; diesel::insert_into(agents::table) .values(agent) @@ -86,7 +86,7 @@ impl UDB { Ok(()) } - pub fn get_agents(&self, uid: Option) -> ULocalResult> { + pub fn get_agents(&self, uid: Option) -> SResult> { use schema::agents; let result = if uid.is_some() { agents::table @@ -98,7 +98,7 @@ impl UDB { Ok(result) } - pub fn update_job_status(&self, uid: Uuid, status: JobState) -> ULocalResult<()> { + pub fn update_job_status(&self, uid: Uuid, status: JobState) -> SResult<()> { use schema::results; diesel::update(results::table) .filter(results::id.eq(uid)) @@ -108,11 +108,7 @@ impl UDB { } //TODO: filters possibly could work in a wrong way, check - pub fn get_exact_jobs( - &self, - uid: Option, - personal: bool, - ) -> ULocalResult> { + pub fn get_exact_jobs(&self, uid: Option, personal: bool) -> SResult> { use schema::results; let mut q = results::table.into_boxed(); /*if uid.is_some() { @@ -134,14 +130,10 @@ impl UDB { Ok(result) } - pub fn set_jobs_for_agent( - &self, - agent_uid: &Uuid, - job_uids: &[Uuid], - ) -> ULocalResult> { + pub fn set_jobs_for_agent(&self, agent_uid: &Uuid, job_uids: &[Uuid]) -> SResult> { use schema::{agents::dsl::agents, jobs::dsl::jobs, results}; if let Err(DslError::NotFound) = agents.find(agent_uid).first::(&self.conn) { - return Err(ULocalError::NotFound(agent_uid.to_string())); + return Err(Error::NotFound(agent_uid.to_string())); } let not_found_jobs = job_uids .iter() @@ -154,7 +146,7 @@ impl UDB { }) .collect::>(); if !not_found_jobs.is_empty() { - return Err(ULocalError::NotFound(not_found_jobs.join(", "))); + return Err(Error::NotFound(not_found_jobs.join(", "))); } let job_requests = job_uids .iter() @@ -174,7 +166,7 @@ impl UDB { Ok(assigned_uids) } - pub fn del_jobs(&self, uids: &[Uuid]) -> ULocalResult { + pub fn del_jobs(&self, uids: &[Uuid]) -> SResult { use schema::jobs; let mut affected = 0; for &uid in uids { @@ -186,7 +178,7 @@ impl UDB { Ok(affected) } - pub fn del_results(&self, uids: &[Uuid]) -> ULocalResult { + pub fn del_results(&self, uids: &[Uuid]) -> SResult { use schema::results; let mut affected = 0; for &uid in uids { @@ -198,7 +190,7 @@ impl UDB { Ok(affected) } - pub fn del_agents(&self, uids: &[Uuid]) -> ULocalResult { + pub fn del_agents(&self, uids: &[Uuid]) -> SResult { use schema::agents; let mut affected = 0; for &uid in uids { diff --git a/bin/u_server/src/errors.rs b/bin/u_server/src/errors.rs new file mode 100644 index 0000000..c77472a --- /dev/null +++ b/bin/u_server/src/errors.rs @@ -0,0 +1,22 @@ +use diesel::result::Error as DslError; +use thiserror::Error; +use warp::reject::Reject; + +pub type SResult = Result; + +#[derive(Error, Debug)] +pub enum Error { + #[error("{0} is not found")] + NotFound(String), + + #[error("Error processing {0}")] + ProcessingError(String), + + #[error(transparent)] + DBError(#[from] DslError), + + #[error("General error: {0}")] + Other(String), +} + +impl Reject for Error {} diff --git a/bin/u_server/src/handlers.rs b/bin/u_server/src/handlers.rs index 9a23f17..d1573cc 100644 --- a/bin/u_server/src/handlers.rs +++ b/bin/u_server/src/handlers.rs @@ -1,19 +1,17 @@ use crate::db::UDB; +use crate::errors::Error; use diesel::SaveChangesDsl; -use hyper::Body; -use serde::Serialize; use u_lib::{ messaging::{AsMsg, BaseMessage, Reportable}, models::*, utils::{OneOrVec, Stripped}, - ULocalError, }; use uuid::Uuid; use warp::{ http::{Response, StatusCode}, Rejection, Reply, }; - +/* pub fn build_response(code: StatusCode, body: impl Into) -> Response { Response::builder().status(code).body(body.into()).unwrap() } @@ -29,116 +27,86 @@ pub fn build_err(body: impl ToString) -> Response { pub fn build_message(m: M) -> Response { warp::reply::json(&m.as_message()).into_response() } +*/ pub struct Endpoints; impl Endpoints { - pub async fn add_agent(msg: Agent) -> Result, Rejection> { - info!("hnd: add_agent"); - UDB::lock_db() - .insert_agent(&msg) - .map(|_| build_ok("")) - .or_else(|e| Ok(build_err(e))) + pub async fn add_agent(msg: Agent) -> Result<(), Rejection> { + UDB::lock_db().insert_agent(&msg).map_err(From::from) } - pub async fn get_agents(uid: Option) -> Result, Rejection> { - info!("hnd: get_agents"); - UDB::lock_db() - .get_agents(uid) - .map(build_message) - .or_else(|e| Ok(build_err(e))) + pub async fn get_agents(uid: Option) -> Result, Rejection> { + UDB::lock_db().get_agents(uid).map_err(From::from) } - pub async fn get_jobs(uid: Option) -> Result, Rejection> { - info!("hnd: get_jobs"); - UDB::lock_db() - .get_jobs(uid) - .map(build_message) - .or_else(|e| Ok(build_err(e))) + pub async fn get_jobs(uid: Option) -> Result, Rejection> { + UDB::lock_db().get_jobs(uid).map_err(From::from) } - pub async fn get_agent_jobs(uid: Option) -> Result, Rejection> { - info!("hnd: get_agent_jobs"); + pub async fn get_agent_jobs(uid: Option) -> Result, Rejection> { UDB::lock_db() .get_exact_jobs(uid, false) - .map(build_message) - .or_else(|e| Ok(build_err(e))) + .map_err(From::from) } - pub async fn get_personal_jobs(uid: Option) -> Result, Rejection> { - info!("hnd: get_personal_jobs"); - let agents = UDB::lock_db().get_agents(uid).unwrap(); + pub async fn get_personal_jobs(uid: Option) -> Result, Rejection> { + let agents = UDB::lock_db().get_agents(uid)?; if agents.is_empty() { let db = UDB::lock_db(); - db.insert_agent(&Agent::with_id(uid.unwrap())).unwrap(); - let job = db.find_job_by_alias("agent_hello").unwrap(); - if let Err(e) = db.set_jobs_for_agent(&uid.unwrap(), &[job.id]) { - return Ok(build_err(e)); - } + db.insert_agent(&Agent::with_id(uid.unwrap()))?; + let job = db.find_job_by_alias("agent_hello")?; + db.set_jobs_for_agent(&uid.unwrap(), &[job.id])?; } let result = UDB::lock_db().get_exact_jobs(uid, true); match result { Ok(r) => { let db = UDB::lock_db(); for j in r.iter() { - db.update_job_status(j.id, JobState::Running).unwrap(); + db.update_job_status(j.id, JobState::Running)?; } - Ok(build_message(r)) + Ok(r) } - Err(e) => Ok(build_err(e)), + Err(e) => Err(e.into()), } } - pub async fn upload_jobs( - msg: BaseMessage<'static, Vec>, - ) -> Result, Rejection> { - info!("hnd: upload_jobs"); + pub async fn upload_jobs(msg: BaseMessage<'static, Vec>) -> Result<(), Rejection> { UDB::lock_db() .insert_jobs(&msg.into_inner()) - .map(|_| build_ok("")) - .or_else(|e| Ok(build_err(e))) + .map_err(From::from) } - pub async fn del(uid: Uuid) -> Result, Rejection> { - info!("hnd: del"); + pub async fn del(uid: Uuid) -> Result { let db = UDB::lock_db(); let del_fns = &[UDB::del_agents, UDB::del_jobs, UDB::del_results]; for del_fn in del_fns { let affected = del_fn(&db, &[uid]).unwrap(); if affected > 0 { - return Ok(build_message(affected as i32)); + return Ok(affected); } } - Ok(build_message(0)) + Ok(0) } pub async fn set_jobs( agent_uid: Uuid, msg: BaseMessage<'static, Vec>, - ) -> Result, Rejection> { - info!("hnd: set_jobs_by_alias, agent: {}", agent_uid); - let jobs: Result, ULocalError> = msg - .into_inner() + ) -> Result, Rejection> { + msg.into_inner() .into_iter() .map(|ident| { - info!("hnd: set_jobs_by_alias, job: {}", ident); Uuid::parse_str(&ident) .or_else(|_| UDB::lock_db().find_job_by_alias(&ident).map(|j| j.id)) }) - .collect(); - match jobs { - Ok(j) => UDB::lock_db() - .set_jobs_for_agent(&agent_uid, &j) - .map(build_message) - .or_else(|e| Ok(build_err(e))), - Err(e) => Ok(build_err(e)), - } + .collect::, Error>>() + .and_then(|j| UDB::lock_db().set_jobs_for_agent(&agent_uid, &j)) + .map_err(From::from) } pub async fn report + AsMsg + 'static>( msg: BaseMessage<'static, Data>, - ) -> Result, Rejection> { - info!("hnd: report"); + ) -> Result<(), Rejection> { let id = msg.id; let mut failed = vec![]; for entry in msg.into_inner().into_vec() { @@ -150,7 +118,7 @@ impl Endpoints { let db = UDB::lock_db(); if let Err(e) = res .save_changes::(&db.conn) - .map_err(ULocalError::from) + .map_err(Error::from) { failed.push(e.to_string()) } @@ -172,9 +140,8 @@ impl Endpoints { } } if !failed.is_empty() { - let err_msg = ULocalError::ProcessingError(failed.join(", ")); - return Ok(build_err(err_msg)); + return Err(Error::ProcessingError(failed.join(", ")).into()); } - Ok(build_ok("")) + Ok(()) } } diff --git a/bin/u_server/src/init.rs b/bin/u_server/src/init.rs index 60b3396..8127227 100644 --- a/bin/u_server/src/init.rs +++ b/bin/u_server/src/init.rs @@ -1,5 +1,5 @@ -use crate::db::UDB; use crate::handlers::Endpoints; +use crate::{db::UDB, errors::SResult}; use serde::de::DeserializeOwned; use std::path::PathBuf; use u_lib::{ @@ -7,7 +7,11 @@ use u_lib::{ models::*, }; use uuid::Uuid; -use warp::{body, Filter, Rejection, Reply}; +use warp::{ + body, + reply::{json, reply, Json}, + Filter, Rejection, Reply, +}; fn get_content() -> impl Filter,), Error = Rejection> + Clone where @@ -16,6 +20,10 @@ where body::content_length_limit(1024 * 64).and(body::json::>()) } +fn into_message(msg: M) -> Json { + json(&msg.as_message()) +} + pub fn init_filters( auth_token: &str, ) -> impl Filter + Clone { @@ -28,12 +36,14 @@ pub fn init_filters( .map(Some) .or_else(infallible_none), ) - .and_then(Endpoints::get_agents); + .and_then(Endpoints::get_agents) + .map(into_message); let upload_jobs = warp::post() .and(warp::path("upload_jobs")) .and(get_content::>()) - .and_then(Endpoints::upload_jobs); + .and_then(Endpoints::upload_jobs) + .map(|_| reply()); let get_jobs = warp::get() .and(warp::path("get_jobs")) @@ -42,7 +52,8 @@ pub fn init_filters( .map(Some) .or_else(infallible_none), ) - .and_then(Endpoints::get_jobs); + .and_then(Endpoints::get_jobs) + .map(into_message); let get_agent_jobs = warp::get() .and(warp::path("get_agent_jobs")) @@ -51,27 +62,33 @@ pub fn init_filters( .map(Some) .or_else(infallible_none), ) - .and_then(Endpoints::get_agent_jobs); + .and_then(Endpoints::get_agent_jobs) + .map(into_message); let get_personal_jobs = warp::get() .and(warp::path("get_personal_jobs")) .and(warp::path::param::().map(Some)) - .and_then(Endpoints::get_personal_jobs); + .and_then(Endpoints::get_personal_jobs) + .map(into_message); let del = warp::get() .and(warp::path("del")) .and(warp::path::param::()) - .and_then(Endpoints::del); + .and_then(Endpoints::del) + .map(|_| reply()); let set_jobs = warp::post() .and(warp::path("set_jobs")) .and(warp::path::param::()) .and(get_content::>()) - .and_then(Endpoints::set_jobs); + .and_then(Endpoints::set_jobs) + .map(into_message); let report = warp::post() .and(warp::path("report")) - .and(get_content::>().and_then(Endpoints::report)); + .and(get_content::>()) + .and_then(Endpoints::report) + .map(|_| reply()); let auth_token = format!("Bearer {auth_token}",).into_boxed_str(); let auth_header = warp::header::exact("authorization", Box::leak(auth_token)); @@ -89,13 +106,13 @@ pub fn init_filters( auth_zone.or(agent_zone) } -pub fn prefill_jobs() { +pub fn prefill_jobs() -> SResult<()> { let agent_hello = JobMeta::builder() .with_type(misc::JobType::Manage) .with_alias("agent_hello") .build() .unwrap(); - UDB::lock_db().insert_jobs(&[agent_hello]).unwrap(); + UDB::lock_db().insert_jobs(&[agent_hello]) } pub fn init_logger() { diff --git a/bin/u_server/src/main.rs b/bin/u_server/src/main.rs index e402a55..84d2139 100644 --- a/bin/u_server/src/main.rs +++ b/bin/u_server/src/main.rs @@ -2,5 +2,5 @@ use u_server_lib::serve; #[tokio::main] async fn main() -> Result<(), String> { - serve().await + serve().await.map_err(|e| e.to_string()) } diff --git a/bin/u_server/src/u_server.rs b/bin/u_server/src/u_server.rs index bf9d8ed..1483acd 100644 --- a/bin/u_server/src/u_server.rs +++ b/bin/u_server/src/u_server.rs @@ -12,9 +12,11 @@ extern crate diesel; // in this block mod db; +mod errors; mod handlers; mod init; +use errors::{Error, SResult}; use init::*; use serde::Deserialize; use std::path::PathBuf; @@ -27,11 +29,11 @@ struct ServEnv { } //TODO: tracing-subscriber -pub async fn serve() -> Result<(), String> { +pub async fn serve() -> SResult<()> { init_logger(); - prefill_jobs(); + prefill_jobs()?; - let env = load_env::().map_err(|e| e.to_string())?; + let env = load_env::().map_err(|e| Error::Other(e.to_string()))?; let routes = init_filters(&env.admin_auth_token); let certs_dir = PathBuf::from("certs"); warp::serve(routes.with(warp::log("warp"))) diff --git a/integration/Cargo.toml b/integration/Cargo.toml index d0c9819..a4ae852 100644 --- a/integration/Cargo.toml +++ b/integration/Cargo.toml @@ -14,7 +14,6 @@ uuid = { version = "0.6.5", features = ["serde", "v4"] } reqwest = { version = "0.11", features = ["json"] } serde_json = "1.0" serde = { version = "1.0.114", features = ["derive"] } -futures = "0.3.5" shlex = "1.0.0" rstest = "0.12" once_cell = "1.10.0" diff --git a/integration/docker-compose.yml b/integration/docker-compose.yml index 2186a8e..40122f3 100644 --- a/integration/docker-compose.yml +++ b/integration/docker-compose.yml @@ -6,13 +6,14 @@ networks: services: u_server: + user: "${DOCKER_UID:-1000}:${DOCKER_GID:-1000}" image: unki/u_server networks: - u_net volumes: - ../release/u_server:/unki/u_server - ../certs:/unki/certs - - ../logs:/unki/logs + - ../logs:/unki/logs:rw working_dir: /unki command: /unki/u_server depends_on: @@ -52,6 +53,7 @@ services: retries: 3 u_agent: + user: "${DOCKER_UID:-1000}:${DOCKER_GID:-1000}" image: unki/u_agent networks: - u_net @@ -67,7 +69,7 @@ services: condition: service_healthy tests_runner: - user: "${DOCKER_UID}:${DOCKER_GID}" + user: "${DOCKER_UID:-1000}:${DOCKER_GID:-1000}" image: unki/tests_runner networks: - u_net diff --git a/integration/tests/helpers/panel.rs b/integration/tests/helpers/panel.rs index 2ddf4f0..27d2639 100644 --- a/integration/tests/helpers/panel.rs +++ b/integration/tests/helpers/panel.rs @@ -1,17 +1,14 @@ use serde::de::DeserializeOwned; use serde_json::{from_slice, Value}; -use shlex::split; -use std::fmt::Display; +use std::fmt::{Debug, Display}; use std::process::{Command, Output}; use u_lib::{ - datatypes::DataResult, + datatypes::PanelResult, utils::{bytes_to_string, ProcOutput}, }; const PANEL_BINARY: &str = "/u_panel"; -type PanelResult = Result, String>; - pub struct Panel; impl Panel { @@ -26,31 +23,40 @@ impl Panel { pub fn output_argv(argv: &[&str]) -> PanelResult { let result = Self::run(argv); let output = ProcOutput::from_output(&result).to_appropriate(); - from_slice(&output).map_err(|e| { - eprintln!( - "Failed to decode panel response: '{}'", - bytes_to_string(&output) - ); - e.to_string() - }) + from_slice(&output) + .map_err(|e| { + eprintln!( + "Failed to decode panel response: '{}'", + bytes_to_string(&output) + ); + e.to_string() + }) + .unwrap() } - pub fn output(args: impl Into + Display) -> PanelResult { - println!("Executing '{PANEL_BINARY} {}'", &args); - let splitted = split(args.into().as_ref()).unwrap(); - Self::output_argv( + pub fn output( + args: impl Into + Display, + ) -> PanelResult { + eprintln!("EXEC >>> {PANEL_BINARY} {}", &args); + let splitted = shlex::split(args.into().as_ref()).unwrap(); + let result = Self::output_argv( splitted .iter() .map(|s| s.as_ref()) .collect::>() .as_ref(), - ) + ); + match &result { + PanelResult::Ok(r) => eprintln!("<<<+ {r:?}"), + PanelResult::Err(e) => eprintln!("<<(data: PanelResult) -> T { - match data.unwrap() { - DataResult::Ok(r) => r, - DataResult::Err(e) => panic!("Panel failed: {}", e), + fn status_is_ok(data: PanelResult) -> T { + match data { + PanelResult::Ok(r) => r, + PanelResult::Err(e) => panic!("Panel failed: {}", e), } } @@ -59,7 +65,7 @@ impl Panel { Self::status_is_ok(result); } - pub fn check_output(args: impl Into + Display) -> T { + pub fn check_output(args: impl Into + Display) -> T { let result = Self::output(args); Self::status_is_ok(result) } diff --git a/integration/tests/lib.rs b/integration/tests/lib.rs index aac47fb..8009650 100644 --- a/integration/tests/lib.rs +++ b/integration/tests/lib.rs @@ -20,7 +20,9 @@ async fn test_non_auth_connection_dropped() { .await { Err(e) => { - assert!(e.to_string().contains("channel closed")) + let err = e.to_string(); + println!("{err}"); + assert!(err.contains("channel closed")) } _ => panic!("no error occured on foreign client connection"), } diff --git a/lib/u_api_proc_macro/src/lib.rs b/lib/u_api_proc_macro/src/lib.rs index 1a400ab..a6d0e89 100644 --- a/lib/u_api_proc_macro/src/lib.rs +++ b/lib/u_api_proc_macro/src/lib.rs @@ -69,20 +69,22 @@ pub fn api_route(args: TokenStream, item: TokenStream) -> TokenStream { Ok(_) => Ok(()), Err(e) => Err(UError::from(e)) }; + let resp = response.text().await?; let result = match is_success { - Ok(_) => response.json::>() - .await - .map(|msg| msg.into_inner()) - .or_else(|e| { - match content_len { - Some(0) => Ok(Default::default()), - _ => Err(UError::from(e)) - } - }), + Ok(_) => { + serde_json::from_str::>(&resp) + .map(|msg| msg.into_inner()) + .or_else(|e| { + match content_len { + Some(0) => Ok(Default::default()), + _ => Err(UError::NetError(e.to_string(), resp.clone())) + } + }) + }, Err(UError::NetError(err_src, _)) => Err( UError::NetError( err_src, - response.text().await? + resp ) ), _ => unreachable!() diff --git a/lib/u_lib/Cargo.toml b/lib/u_lib/Cargo.toml index da445e7..972645e 100644 --- a/lib/u_lib/Cargo.toml +++ b/lib/u_lib/Cargo.toml @@ -24,15 +24,14 @@ once_cell = "1.7.2" shlex = "1.0.0" u_api_proc_macro = { version = "*", path = "../u_api_proc_macro" } crossbeam = "0.8.1" -backtrace = "0.3.61" diesel = { version = "1.4.5", features = ["postgres", "uuid"] } envy = "0.4.2" +serde_json = "1.0.81" [target.'cfg(not(target_arch = "wasm32"))'.dependencies] reqwest = { version = "0.11", features = ["json", "native-tls"] } tokio = { version = "1.2.0", features = ["rt-multi-thread", "sync", "macros", "process", "time"] } guess_host_triple = "0.1.2" -openssl = "*" [dev-dependencies] rstest = "0.12" diff --git a/lib/u_lib/src/builder.rs b/lib/u_lib/src/builder.rs index 29aeef1..c9efcb0 100644 --- a/lib/u_lib/src/builder.rs +++ b/lib/u_lib/src/builder.rs @@ -4,7 +4,7 @@ use crate::{ messaging::Reportable, models::{Agent, AssignedJob, JobMeta, JobType}, utils::{CombinedResult, OneOrVec}, - UError, UErrorBt, UResult, + UError, UResult, }; use guess_host_triple::guess_host_triple; use std::collections::HashMap; @@ -17,11 +17,11 @@ impl JobBuilder { pub fn from_request(job_requests: impl OneOrVec) -> CombinedResult { let job_requests = job_requests.into_vec(); let mut prepared: Vec = vec![]; - let mut result = CombinedResult::::new(); + let mut result = CombinedResult::::new(); for req in job_requests { let job_meta = JobCache::get(req.job_id); if job_meta.is_none() { - result.err(UError::NoJob(req.job_id).into_bt()); + result.err(UError::NoJob(req.job_id)); continue; } let job_meta = job_meta.unwrap(); @@ -288,7 +288,7 @@ mod tests { job = job.with_payload(p); } let err = job.build().unwrap_err(); - let err_msg = unwrap_enum!(err.err, UError::JobArgsError); + let err_msg = unwrap_enum!(err, UError::JobArgsError); assert!(err_msg.contains(err_str)); Ok(()) } diff --git a/lib/u_lib/src/datatypes.rs b/lib/u_lib/src/datatypes.rs index 983747f..f7d09a6 100644 --- a/lib/u_lib/src/datatypes.rs +++ b/lib/u_lib/src/datatypes.rs @@ -1,10 +1,10 @@ -use crate::UErrorBt; +use crate::UError; use serde::{Deserialize, Serialize}; #[derive(Serialize, Deserialize)] #[serde(rename_all = "lowercase")] #[serde(tag = "status", content = "data")] -pub enum DataResult { +pub enum PanelResult { Ok(M), - Err(UErrorBt), + Err(UError), } diff --git a/lib/u_lib/src/errors/chan.rs b/lib/u_lib/src/errors/chan.rs index cf78fd0..1c2b995 100644 --- a/lib/u_lib/src/errors/chan.rs +++ b/lib/u_lib/src/errors/chan.rs @@ -1,8 +1,8 @@ -use crate::UErrorBt; +use crate::UError; use crossbeam::channel::{self, Receiver, Sender}; use once_cell::sync::OnceCell; -type ChanError = UErrorBt; +type ChanError = UError; static ERR_CHAN: OnceCell = OnceCell::new(); pub struct ErrChan { @@ -18,8 +18,8 @@ impl ErrChan { }) } - pub fn send(msg: impl Into) { - Self::get().tx.send(msg.into()).unwrap() + pub fn send(msg: ChanError) { + Self::get().tx.send(msg).unwrap() } pub fn recv() -> ChanError { diff --git a/lib/u_lib/src/errors/variants.rs b/lib/u_lib/src/errors/variants.rs index 103afe1..829d7ab 100644 --- a/lib/u_lib/src/errors/variants.rs +++ b/lib/u_lib/src/errors/variants.rs @@ -1,35 +1,10 @@ -use backtrace::Backtrace as CrateBacktrace; -use diesel::result::Error as DslError; #[cfg(not(target_arch = "wasm32"))] use reqwest::Error as ReqError; use serde::{Deserialize, Serialize}; -use std::fmt; use thiserror::Error; use uuid::Uuid; -pub type UResult = std::result::Result; -pub type ULocalResult = std::result::Result; - -#[derive(Error, Debug, Serialize, Deserialize, Clone)] -pub struct UErrorBt { - pub err: UError, - pub backtrace: String, -} - -impl fmt::Display for UErrorBt { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "{}\nBACKTRACE: \n{:?}", self.err, self.backtrace) - } -} - -impl From for UErrorBt { - fn from(err: UError) -> UErrorBt { - UErrorBt { - err, - backtrace: format!("{:?}", CrateBacktrace::new()), - } - } -} +pub type UResult = std::result::Result; #[derive(Error, Debug, Serialize, Deserialize, Clone)] pub enum UError { @@ -70,34 +45,9 @@ pub enum UError { PanelError(String), } -impl UError { - pub fn into_bt(self) -> UErrorBt { - UErrorBt::from(self) - } -} - -#[cfg(not(target_arch = "wasm32"))] -impl From for UErrorBt { - fn from(e: ReqError) -> Self { - UError::from(e).into_bt() - } -} - #[cfg(not(target_arch = "wasm32"))] impl From for UError { fn from(e: ReqError) -> Self { UError::NetError(e.to_string(), String::new()) } } - -#[derive(Error, Debug)] -pub enum ULocalError { - #[error("{0} is not found")] - NotFound(String), - - #[error("Error processing {0}")] - ProcessingError(String), - - #[error(transparent)] - DBError(#[from] DslError), -} diff --git a/lib/u_lib/src/lib.rs b/lib/u_lib/src/lib.rs index b49f71d..2b6fa07 100644 --- a/lib/u_lib/src/lib.rs +++ b/lib/u_lib/src/lib.rs @@ -25,7 +25,7 @@ pub mod exports { } pub use config::UID; -pub use errors::{UError, UErrorBt, ULocalError, ULocalResult, UResult}; +pub use errors::{UError, UResult}; pub use exports::*; pub mod schema_exports { diff --git a/lib/u_lib/src/messaging/base.rs b/lib/u_lib/src/messaging/base.rs index 8ac1158..3cef82d 100644 --- a/lib/u_lib/src/messaging/base.rs +++ b/lib/u_lib/src/messaging/base.rs @@ -8,10 +8,7 @@ use uuid::Uuid; pub struct Moo<'cow, T: AsMsg + Clone>(pub Cow<'cow, T>); pub trait AsMsg: Clone + Serialize { - fn as_message<'m>(&'m self) -> BaseMessage<'m, Self> - where - Moo<'m, Self>: From<&'m Self>, - { + fn as_message(&self) -> BaseMessage<'_, Self> { BaseMessage::new(self) } } diff --git a/lib/u_lib/src/models/jobs/assigned.rs b/lib/u_lib/src/models/jobs/assigned.rs index 8893977..052f02b 100644 --- a/lib/u_lib/src/models/jobs/assigned.rs +++ b/lib/u_lib/src/models/jobs/assigned.rs @@ -84,11 +84,7 @@ impl AssignedJob { if let Some(ref payload) = meta.payload { let extracted_payload = match TempFile::write_exec(payload) { Ok(p) => p, - Err(e) => { - return Reportable::Error( - UError::Runtime(e.to_string()).into_bt().to_string(), - ) - } + Err(e) => return Reportable::Error(UError::Runtime(e.to_string()).to_string()), }; ( meta.argv.replace("{}", &extracted_payload.get_path()), diff --git a/lib/u_lib/src/utils/combined_result.rs b/lib/u_lib/src/utils/combined_result.rs index 4464c39..05b92d1 100644 --- a/lib/u_lib/src/utils/combined_result.rs +++ b/lib/u_lib/src/utils/combined_result.rs @@ -1,7 +1,7 @@ use crate::utils::OneOrVec; -use crate::UErrorBt; +use crate::UError; -pub struct CombinedResult { +pub struct CombinedResult { ok: Vec, err: Vec, }