From cf7563da3c5abc6e73f837585345e002e29802de Mon Sep 17 00:00:00 2001 From: Igor Bubelov Date: Sat, 12 Oct 2024 18:13:48 +0700 Subject: [PATCH] Improve error handling --- Cargo.lock | 1 + Cargo.toml | 1 + src/command/admin.rs | 8 +++++--- src/command/area.rs | 13 ++++++++----- src/command/common.rs | 2 +- src/command/element.rs | 24 ++++++++++++++---------- src/command/report.rs | 11 +++++++---- src/command/user.rs | 3 ++- src/rpc.rs | 32 +++++++++++++++++++++++++++----- 9 files changed, 66 insertions(+), 29 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 5d47fd8..8bfe4c3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -95,6 +95,7 @@ dependencies = [ "colored_json", "dirs", "rusqlite", + "serde", "serde_json", "ureq", ] diff --git a/Cargo.toml b/Cargo.toml index 5d33008..ea09134 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,3 +16,4 @@ rusqlite = { version = "0.32.1", features = ["bundled"] } dirs = "5.0.1" serde_json = "1.0.128" clap = { version = "4.5.20", features = ["derive"] } +serde = { version = "1.0.210", features = ["derive"] } diff --git a/src/command/admin.rs b/src/command/admin.rs index 77e048f..e1e73ba 100644 --- a/src/command/admin.rs +++ b/src/command/admin.rs @@ -12,7 +12,7 @@ pub fn add_admin(args: &AddAdminArgs) -> Result<()> { rpc::call( "add_admin", json!({"new_admin_name": args.new_admin_name, "new_admin_password": args.new_admin_password}), - ) + )?.print() } #[derive(Args)] @@ -25,7 +25,8 @@ pub fn add_allowed_action(args: &AddAllowedActionArgs) -> Result<()> { rpc::call( "add_allowed_action", json!({"admin_name": args.admin_name, "action": args.action}), - ) + )? + .print() } #[derive(Args)] @@ -38,5 +39,6 @@ pub fn remove_allowed_action(args: &RemoveAllowedActionArgs) -> Result<()> { rpc::call( "remove_allowed_action", json!({"admin_name": args.admin_name, "action": args.action}), - ) + )? + .print() } diff --git a/src/command/area.rs b/src/command/area.rs index bdee136..702c717 100644 --- a/src/command/area.rs +++ b/src/command/area.rs @@ -8,7 +8,7 @@ pub struct GetAreaArgs { } pub fn get_area(args: &GetAreaArgs) -> Result<()> { - rpc::call("get_area", json!({"id": args.id})) + rpc::call("get_area", json!({"id": args.id}))?.print() } #[derive(Args)] @@ -23,7 +23,8 @@ pub fn set_area_tag(args: &SetAreaTagArgs) -> Result<()> { rpc::call( "set_area_tag", json!({"id": args.id,"name": args.name, "value": value}), - ) + )? + .print() } #[derive(Args)] @@ -33,7 +34,7 @@ pub struct RemoveAreaTagArgs { } pub fn remove_area_tag(args: &RemoveAreaTagArgs) -> Result<()> { - rpc::call("remove_area_tag", json!({"id": args.id,"tag": args.tag})) + rpc::call("remove_area_tag", json!({"id": args.id,"tag": args.tag}))?.print() } #[derive(Args)] @@ -47,7 +48,8 @@ pub fn set_area_icon(args: &SetAreaIconArgs) -> Result<()> { rpc::call( "set_area_icon", json!({"id": args.id,"icon_base64": args.icon_base64,"icon_ext": args.icon_ext}), - ) + )? + .print() } #[derive(Args)] @@ -60,5 +62,6 @@ pub fn generate_areas_elements_mapping(args: &GenerateAreasElementsMappingArgs) rpc::call( "generate_areas_elements_mapping", json!({"from_element_id": args.from_element_id,"to_element_id": args.to_element_id}), - ) + )? + .print() } diff --git a/src/command/common.rs b/src/command/common.rs index 3d22af8..3731561 100644 --- a/src/command/common.rs +++ b/src/command/common.rs @@ -8,5 +8,5 @@ pub struct SearchArgs { } pub fn search(args: &SearchArgs) -> Result<()> { - rpc::call("search", json!({"query": args.query})) + rpc::call("search", json!({"query": args.query}))?.print() } diff --git a/src/command/element.rs b/src/command/element.rs index 3b5d80f..8009289 100644 --- a/src/command/element.rs +++ b/src/command/element.rs @@ -8,7 +8,7 @@ pub struct GetElementArgs { } pub fn get_element(args: &GetElementArgs) -> Result<()> { - rpc::call("get_element", json!({"id": args.id})) + rpc::call("get_element", json!({"id": args.id}))?.print() } #[derive(Args)] @@ -23,7 +23,8 @@ pub fn set_element_tag(args: &SetElementTagArgs) -> Result<()> { rpc::call( "set_element_tag", json!({"id": args.id,"name": args.name, "value": value}), - ) + )? + .print() } #[derive(Args)] @@ -33,7 +34,7 @@ pub struct RemoveElementTagArgs { } pub fn remove_element_tag(args: &RemoveElementTagArgs) -> Result<()> { - rpc::call("remove_element_tag", json!({"id": args.id,"tag": args.tag})) + rpc::call("remove_element_tag", json!({"id": args.id,"tag": args.tag}))?.print() } #[derive(Args)] @@ -46,7 +47,8 @@ pub fn add_element_comment(args: &AddElementCommentArgs) -> Result<()> { rpc::call( "add_element_comment", json!({"id": args.id,"comment": args.comment}), - ) + )? + .print() } #[derive(Args)] @@ -56,21 +58,21 @@ pub struct BoostElementArgs { } pub fn boost_element(args: &BoostElementArgs) -> Result<()> { - rpc::call("boost_element", json!({"id": args.id,"days": args.days})) + rpc::call("boost_element", json!({"id": args.id,"days": args.days}))?.print() } #[derive(Args)] pub struct GetBoostsArgs {} pub fn get_boosts(_: &GetBoostsArgs) -> Result<()> { - rpc::call("get_boosts", json!({})) + rpc::call("get_boosts", json!({}))?.print() } #[derive(Args)] pub struct SyncElementsArgs {} pub fn sync_elements(_: &SyncElementsArgs) -> Result<()> { - rpc::call("sync_elements", json!({})) + rpc::call("sync_elements", json!({}))?.print() } #[derive(Args)] @@ -83,7 +85,8 @@ pub fn generate_element_icons(args: &GenerateElementIconsArgs) -> Result<()> { rpc::call( "generate_element_icons", json!({"from_element_id": args.from_element_id,"to_element_id": args.to_element_id}), - ) + )? + .print() } #[derive(Args)] @@ -96,12 +99,13 @@ pub fn generate_element_categories(args: &GenerateElementCategoriesArgs) -> Resu rpc::call( "generate_element_categories", json!({"from_element_id": args.from_element_id,"to_element_id": args.to_element_id}), - ) + )? + .print() } #[derive(Args)] pub struct GenerateElementIssuesArgs {} pub fn generate_element_issues(_: &GenerateElementIssuesArgs) -> Result<()> { - rpc::call("generate_element_issues", json!({})) + rpc::call("generate_element_issues", json!({}))?.print() } diff --git a/src/command/report.rs b/src/command/report.rs index e3481ef..c4ea5c3 100644 --- a/src/command/report.rs +++ b/src/command/report.rs @@ -6,7 +6,7 @@ use serde_json::json; pub struct GenerateReportsArgs {} pub fn generate_reports(_: &GenerateReportsArgs) -> Result<()> { - rpc::call("generate_reports", json!({})) + rpc::call("generate_reports", json!({}))?.print() } #[derive(Args)] @@ -19,7 +19,8 @@ pub fn get_trending_countries(args: &GetTrendingCountriesArgs) -> Result<()> { rpc::call( "get_trending_countries", json!({"period_start": args.period_start, "period_end": args.period_end}), - ) + )? + .print() } #[derive(Args)] @@ -32,7 +33,8 @@ pub fn get_trending_communities(args: &GetTrendingCommunitiesArgs) -> Result<()> rpc::call( "get_trending_communities", json!({"period_start": args.period_start, "period_end": args.period_end}), - ) + )? + .print() } #[derive(Args)] @@ -45,5 +47,6 @@ pub fn get_most_commented_countries(args: &GetMostCommentedCountriesArgs) -> Res rpc::call( "get_most_commented_countries", json!({"period_start": args.period_start, "period_end": args.period_end}), - ) + )? + .print() } diff --git a/src/command/user.rs b/src/command/user.rs index fb6b343..31ba9a7 100644 --- a/src/command/user.rs +++ b/src/command/user.rs @@ -12,5 +12,6 @@ pub fn get_user_activity(args: &GetUserActivityArgs) -> Result<()> { rpc::call( "get_user_activity", json!({"id": args.id, "limit": args.limit}), - ) + )? + .print() } diff --git a/src/rpc.rs b/src/rpc.rs index e8be106..1487f69 100644 --- a/src/rpc.rs +++ b/src/rpc.rs @@ -1,9 +1,32 @@ use crate::settings; use crate::Result; use colored_json::ToColoredJson; +use serde::Deserialize; use serde_json::{json, Value}; -pub fn call(method: &str, mut params: Value) -> Result<()> { +#[derive(Deserialize)] +pub struct RpcResponse { + pub result: Option, + pub error: Option, +} + +impl RpcResponse { + pub fn print(&self) -> Result<()> { + if let Some(result) = &self.result { + println!( + "{}", + serde_json::to_string(&result)?.to_colored_json_auto()? + ); + } else { + if let Some(error) = &self.error { + println!("{}", serde_json::to_string(&error)?.to_colored_json_auto()?) + } + } + Ok(()) + } +} + +pub fn call(method: &str, mut params: Value) -> Result { let params = params .as_object_mut() .ok_or("params value is not a valid JSON object")?; @@ -15,10 +38,9 @@ pub fn call(method: &str, mut params: Value) -> Result<()> { if api_url.trim().is_empty() { api_url = "https://api.btcmap.org/rpc".into(); } - let res = ureq::post(api_url) + let response: RpcResponse = ureq::post(api_url) .send_json(args)? .body_mut() - .read_to_string()?; - println!("{}", res.to_colored_json_auto()?); - Ok(()) + .read_json()?; + Ok(response) }