Skip to content

Commit

Permalink
grin v5.3 (0107) Thiserror changeover (mimblewimble#3728)
Browse files Browse the repository at this point in the history
  • Loading branch information
bayk committed Jun 21, 2024
1 parent 298396d commit ad292d5
Show file tree
Hide file tree
Showing 122 changed files with 3,228 additions and 3,177 deletions.
2,387 changes: 1,445 additions & 942 deletions Cargo.lock

Large diffs are not rendered by default.

6 changes: 2 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,13 @@ blake2-rfc = "0.2"
chrono = "0.4.11"
clap = { version = "2.33", features = ["yaml"] }
ctrlc = { version = "3.1", features = ["termination"] }
failure = "0.1"
failure_derive = "0.1"
futures = "0.3.19"
humansize = "1.1.0"
log = "0.4"
serde = "1"
serde_json = "1"
term = "0.6"
cursive_table_view = "0.13.2"
cursive_table_view = "0.14.0"

grin_api = { path = "./api", version = "5.2.0" }
grin_config = { path = "./config", version = "5.2.0" }
Expand All @@ -44,7 +42,7 @@ grin_servers = { path = "./servers", version = "5.2.0" }
grin_util = { path = "./util", version = "5.2.0" }

[dependencies.cursive]
version = "0.16"
version = "0.20"
default-features = false
features = ["pancurses-backend"]

Expand Down
3 changes: 1 addition & 2 deletions api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,14 @@ edition = "2018"

[dependencies]
easy-jsonrpc-mw = "0.5.4"
failure = "0.1.1"
failure_derive = "0.1.1"
hyper = "0.13"
lazy_static = "1"
regex = "1"
ring = "0.16"
serde = "1"
serde_derive = "1"
serde_json = "1"
thiserror = "1"
log = "0.4"
tokio = { version = "0.2", features = ["full"] }
tokio-rustls = "0.13"
Expand Down
38 changes: 16 additions & 22 deletions api/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@
//! High level JSON/HTTP client API
use crate::core::global;
use crate::rest::{Error, ErrorKind};
use crate::rest::Error;
use crate::util::to_base64;
use failure::Fail;
use http::uri::{InvalidUri, Uri};
use http::uri::Uri;
use hyper::body;
use hyper::header::{ACCEPT, AUTHORIZATION, CONTENT_TYPE, USER_AGENT};
use hyper::{Body, Client, Request};
Expand Down Expand Up @@ -192,10 +191,9 @@ fn build_request_ex(
body: Option<String>,
) -> Result<Request<Body>, Error> {
// Checking only. Uri has issues with Builder 'uri()' method
let _ = url.parse::<Uri>().map_err::<Error, _>(|e: InvalidUri| {
e.context(ErrorKind::Argument(format!("Invalid url {}", url)))
.into()
})?;
let _ = url
.parse::<Uri>()
.map_err::<Error, _>(|e| Error::Argument(format!("Invalid url {}, {}", url, e)))?;

let mut builder = Request::builder();

Expand All @@ -221,9 +219,7 @@ fn build_request_ex(
None => Body::empty(),
Some(json) => json.into(),
})
.map_err(|e| {
ErrorKind::RequestError(format!("Bad request {} {}: {}", method, url, e)).into()
})
.map_err(|e| Error::RequestError(format!("Bad request {} {}: {}", method, url, e)))
}

pub fn create_post_request<IN>(
Expand All @@ -235,7 +231,7 @@ where
IN: Serialize,
{
let json = serde_json::to_string(input).map_err(|e| {
ErrorKind::Internal(format!("Post Request, Can't serialize data to JSON, {}", e))
Error::Internal(format!("Post Request, Can't serialize data to JSON, {}", e))
})?;
build_request(url, "POST", api_secret, Some(json))
}
Expand All @@ -250,7 +246,7 @@ where
IN: Serialize,
{
let json = serde_json::to_string(input).map_err(|e| {
ErrorKind::Internal(format!("Post Request, Can't serialize data to JSON, {}", e))
Error::Internal(format!("Post Request, Can't serialize data to JSON, {}", e))
})?;
build_request_ex(url, "POST", api_secret, basic_auth_key, Some(json))
}
Expand All @@ -260,9 +256,8 @@ where
for<'de> T: Deserialize<'de>,
{
let data = send_request(req, timeout)?;
serde_json::from_str(&data).map_err(|e| {
ErrorKind::ResponseError(format!("Cannot parse response: {}, {}", data, e)).into()
})
serde_json::from_str(&data)
.map_err(|e| Error::ResponseError(format!("Cannot parse response: {}, {}", data, e)))
}

async fn handle_request_async<T>(req: Request<Body>) -> Result<T, Error>
Expand All @@ -271,7 +266,7 @@ where
{
let data = send_request_async(req, TimeOut::default()).await?;
let ser = serde_json::from_str(&data)
.map_err(|e| ErrorKind::ResponseError(format!("Cannot parse response: {}, {}", data, e)))?;
.map_err(|e| Error::ResponseError(format!("Cannot parse response: {}, {}", data, e)))?;
Ok(ser)
}

Expand All @@ -291,22 +286,21 @@ async fn send_request_async(req: Request<Body>, timeout: TimeOut) -> Result<Stri
let resp = client
.request(req)
.await
.map_err(|e| ErrorKind::RequestError(format!("Cannot make request: {}", e)))?;
.map_err(|e| Error::RequestError(format!("Cannot make request: {}", e)))?;

let status = resp.status().clone();

// Read body first because we want to return it in case of error.
let raw = body::to_bytes(resp)
.await
.map_err(|e| ErrorKind::RequestError(format!("Cannot read response body: {}", e)))?;
.map_err(|e| Error::RequestError(format!("Cannot read response body: {}", e)))?;
let response_body = String::from_utf8_lossy(&raw).to_string();

if !status.is_success() {
return Err(ErrorKind::RequestError(format!(
return Err(Error::RequestError(format!(
"Wrong response code: {} with data {}",
status, response_body
))
.into());
)));
}
Ok(response_body)
}
Expand All @@ -316,6 +310,6 @@ pub fn send_request(req: Request<Body>, timeout: TimeOut) -> Result<String, Erro
.basic_scheduler()
.enable_all()
.build()
.map_err(|e| ErrorKind::Internal(format!("can't create Tokio runtime, {}", e)))?;
.map_err(|e| Error::Internal(format!("can't create Tokio runtime, {}", e)))?;
rt.block_on(send_request_async(req, timeout))
}
Loading

0 comments on commit ad292d5

Please sign in to comment.