From e6a180e9eed4273db24a59f8e06afa117cfe39a9 Mon Sep 17 00:00:00 2001 From: Bryan Newbold Date: Fri, 4 Nov 2022 17:45:08 -0700 Subject: cli: slightly better error handling --- adenosine-cli/src/lib.rs | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/adenosine-cli/src/lib.rs b/adenosine-cli/src/lib.rs index 8ffeb88..a3f1dac 100644 --- a/adenosine-cli/src/lib.rs +++ b/adenosine-cli/src/lib.rs @@ -67,10 +67,17 @@ impl XrpcClient { .get(format!("{}/xrpc/{}", self.host, nsid)) .query(¶ms) .send()?; + // TODO: refactor this error handling stuff into single method if res.status() == 400 { let val: Value = res.json()?; return Err(anyhow!( - "XRPC Bad Request: {}", + "XRPC Bad Request (400): {}", + val["message"].as_str().unwrap_or("unknown") + )); + } else if res.status() == 500 { + let val: Value = res.json()?; + return Err(anyhow!( + "XRPC Internal Error (500): {}", val["message"].as_str().unwrap_or("unknown") )); } @@ -93,7 +100,13 @@ impl XrpcClient { if res.status() == 400 { let val: Value = res.json()?; return Err(anyhow!( - "XRPC Bad Request: {}", + "XRPC Bad Request (400): {}", + val["message"].as_str().unwrap_or("unknown") + )); + } else if res.status() == 500 { + let val: Value = res.json()?; + return Err(anyhow!( + "XRPC Internal Error (500): {}", val["message"].as_str().unwrap_or("unknown") )); } @@ -121,7 +134,13 @@ impl XrpcClient { if res.status() == 400 { let val: Value = res.json()?; return Err(anyhow!( - "XRPC Bad Request: {}", + "XRPC Bad Request (400): {}", + val["message"].as_str().unwrap_or("unknown") + )); + } else if res.status() == 500 { + let val: Value = res.json()?; + return Err(anyhow!( + "XRPC Internal Error (500): {}", val["message"].as_str().unwrap_or("unknown") )); } -- cgit v1.2.3