Skip to content

Commit

Permalink
Improve error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
bubelov committed Oct 12, 2024
1 parent 93776c8 commit cf7563d
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 29 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
8 changes: 5 additions & 3 deletions src/command/admin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand All @@ -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)]
Expand All @@ -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()
}
13 changes: 8 additions & 5 deletions src/command/area.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand All @@ -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)]
Expand All @@ -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)]
Expand All @@ -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)]
Expand All @@ -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()
}
2 changes: 1 addition & 1 deletion src/command/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
24 changes: 14 additions & 10 deletions src/command/element.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand All @@ -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)]
Expand All @@ -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)]
Expand All @@ -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)]
Expand All @@ -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)]
Expand All @@ -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)]
Expand All @@ -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()
}
11 changes: 7 additions & 4 deletions src/command/report.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand All @@ -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)]
Expand All @@ -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)]
Expand All @@ -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()
}
3 changes: 2 additions & 1 deletion src/command/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
32 changes: 27 additions & 5 deletions src/rpc.rs
Original file line number Diff line number Diff line change
@@ -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<Value>,
pub error: Option<Value>,
}

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<RpcResponse> {
let params = params
.as_object_mut()
.ok_or("params value is not a valid JSON object")?;
Expand All @@ -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)
}

0 comments on commit cf7563d

Please sign in to comment.