Skip to content

Commit

Permalink
Thiserror changeover (#3728)
Browse files Browse the repository at this point in the history
* WIP remove failure from all `Cargo.toml`

* WIP remove `extern crate failure_derive`

* Use `thiserror` to fix all errors

* StoreErr is still a tuple

* Remove another set of unnecessary `.into()`s

* update fuzz tests

* update pool/fuzz dependencies in cargo.lock

* small changes based on feedback

Co-authored-by: trevyn <trevyn-git@protonmail.com>
  • Loading branch information
yeastplume and trevyn authored Jul 14, 2022
1 parent 03b007c commit a14a8e3
Show file tree
Hide file tree
Showing 75 changed files with 1,780 additions and 2,194 deletions.
527 changes: 270 additions & 257 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 0 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ futures = "0.3.19"
serde_json = "1"
log = "0.4"
term = "0.6"
failure = "0.1"
failure_derive = "0.1"

grin_api = { path = "./api", version = "5.2.0-alpha.1" }
grin_config = { path = "./config", version = "5.2.0-alpha.1" }
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
28 changes: 11 additions & 17 deletions api/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@

//! High level JSON/HTTP client API
use crate::rest::{Error, ErrorKind};
use crate::rest::Error;
use crate::util::to_base64;
use failure::{Fail, ResultExt};
use hyper::body;
use hyper::header::{ACCEPT, AUTHORIZATION, CONTENT_TYPE, USER_AGENT};
use hyper::{Body, Client, Request};
Expand Down Expand Up @@ -181,9 +180,7 @@ fn build_request(
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 @@ -194,9 +191,8 @@ pub fn create_post_request<IN>(
where
IN: Serialize,
{
let json = serde_json::to_string(input).context(ErrorKind::Internal(
"Could not serialize data to JSON".to_owned(),
))?;
let json = serde_json::to_string(input)
.map_err(|e| Error::Internal(format!("Could not serialize data to JSON: {}", e)))?;
build_request(url, "POST", api_secret, Some(json))
}

Expand All @@ -205,10 +201,8 @@ where
for<'de> T: Deserialize<'de>,
{
let data = send_request(req, timeout)?;
serde_json::from_str(&data).map_err(|e| {
e.context(ErrorKind::ResponseError("Cannot parse response".to_owned()))
.into()
})
serde_json::from_str(&data)
.map_err(|e| Error::ResponseError(format!("Cannot parse response {}", e)))
}

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

Expand All @@ -237,10 +231,10 @@ 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)))?;

if !resp.status().is_success() {
return Err(ErrorKind::RequestError(format!(
return Err(Error::RequestError(format!(
"Wrong response code: {} with data {:?}",
resp.status(),
resp.body()
Expand All @@ -250,7 +244,7 @@ async fn send_request_async(req: Request<Body>, timeout: TimeOut) -> Result<Stri

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)))?;

Ok(String::from_utf8_lossy(&raw).to_string())
}
Expand All @@ -260,6 +254,6 @@ pub fn send_request(req: Request<Body>, timeout: TimeOut) -> Result<String, Erro
.basic_scheduler()
.enable_all()
.build()
.map_err(|e| ErrorKind::RequestError(format!("{}", e)))?;
.map_err(|e| Error::RequestError(format!("{}", e)))?;
rt.block_on(send_request_async(req, timeout))
}
79 changes: 38 additions & 41 deletions api/src/foreign_rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use crate::core::core::transaction::Transaction;
use crate::foreign::Foreign;
use crate::pool::PoolEntry;
use crate::pool::{BlockChain, PoolAdapter};
use crate::rest::ErrorKind;
use crate::rest::Error;
use crate::types::{
BlockHeaderPrintable, BlockPrintable, LocatedTxKernel, OutputListing, OutputPrintable, Tip,
Version,
Expand Down Expand Up @@ -126,7 +126,7 @@ pub trait ForeignRpc: Sync + Send {
height: Option<u64>,
hash: Option<String>,
commit: Option<String>,
) -> Result<BlockHeaderPrintable, ErrorKind>;
) -> Result<BlockHeaderPrintable, Error>;

/**
Networked version of [Foreign::get_block](struct.Foreign.html#method.get_block).
Expand Down Expand Up @@ -244,7 +244,7 @@ pub trait ForeignRpc: Sync + Send {
height: Option<u64>,
hash: Option<String>,
commit: Option<String>,
) -> Result<BlockPrintable, ErrorKind>;
) -> Result<BlockPrintable, Error>;

/**
Networked version of [Foreign::get_version](struct.Foreign.html#method.get_version).
Expand Down Expand Up @@ -277,7 +277,7 @@ pub trait ForeignRpc: Sync + Send {
# );
```
*/
fn get_version(&self) -> Result<Version, ErrorKind>;
fn get_version(&self) -> Result<Version, Error>;

/**
Networked version of [Foreign::get_tip](struct.Foreign.html#method.get_tip).
Expand Down Expand Up @@ -312,7 +312,7 @@ pub trait ForeignRpc: Sync + Send {
# );
```
*/
fn get_tip(&self) -> Result<Tip, ErrorKind>;
fn get_tip(&self) -> Result<Tip, Error>;

/**
Networked version of [Foreign::get_kernel](struct.Foreign.html#method.get_kernel).
Expand Down Expand Up @@ -355,7 +355,7 @@ pub trait ForeignRpc: Sync + Send {
excess: String,
min_height: Option<u64>,
max_height: Option<u64>,
) -> Result<LocatedTxKernel, ErrorKind>;
) -> Result<LocatedTxKernel, Error>;

/**
Networked version of [Foreign::get_outputs](struct.Foreign.html#method.get_outputs).
Expand Down Expand Up @@ -442,7 +442,7 @@ pub trait ForeignRpc: Sync + Send {
end_height: Option<u64>,
include_proof: Option<bool>,
include_merkle_proof: Option<bool>,
) -> Result<Vec<OutputPrintable>, ErrorKind>;
) -> Result<Vec<OutputPrintable>, Error>;

/**
Networked version of [Foreign::get_unspent_outputs](struct.Foreign.html#method.get_unspent_outputs).
Expand Down Expand Up @@ -503,7 +503,7 @@ pub trait ForeignRpc: Sync + Send {
end_index: Option<u64>,
max: u64,
include_proof: Option<bool>,
) -> Result<OutputListing, ErrorKind>;
) -> Result<OutputListing, Error>;

/**
Networked version of [Foreign::get_pmmr_indices](struct.Foreign.html#method.get_pmmr_indices).
Expand Down Expand Up @@ -540,7 +540,7 @@ pub trait ForeignRpc: Sync + Send {
&self,
start_block_height: u64,
end_block_height: Option<u64>,
) -> Result<OutputListing, ErrorKind>;
) -> Result<OutputListing, Error>;

/**
Networked version of [Foreign::get_pool_size](struct.Foreign.html#method.get_pool_size).
Expand Down Expand Up @@ -570,7 +570,7 @@ pub trait ForeignRpc: Sync + Send {
# );
```
*/
fn get_pool_size(&self) -> Result<usize, ErrorKind>;
fn get_pool_size(&self) -> Result<usize, Error>;

/**
Networked version of [Foreign::get_stempool_size](struct.Foreign.html#method.get_stempool_size).
Expand Down Expand Up @@ -600,7 +600,7 @@ pub trait ForeignRpc: Sync + Send {
# );
```
*/
fn get_stempool_size(&self) -> Result<usize, ErrorKind>;
fn get_stempool_size(&self) -> Result<usize, Error>;

/**
Networked version of [Foreign::get_unconfirmed_transactions](struct.Foreign.html#method.get_unconfirmed_transactions).
Expand Down Expand Up @@ -673,7 +673,7 @@ pub trait ForeignRpc: Sync + Send {
# );
```
*/
fn get_unconfirmed_transactions(&self) -> Result<Vec<PoolEntry>, ErrorKind>;
fn get_unconfirmed_transactions(&self) -> Result<Vec<PoolEntry>, Error>;

/**
Networked version of [Foreign::push_transaction](struct.Foreign.html#method.push_transaction).
Expand Down Expand Up @@ -738,7 +738,7 @@ pub trait ForeignRpc: Sync + Send {
# );
```
*/
fn push_transaction(&self, tx: Transaction, fluff: Option<bool>) -> Result<(), ErrorKind>;
fn push_transaction(&self, tx: Transaction, fluff: Option<bool>) -> Result<(), Error>;
}

impl<B, P> ForeignRpc for Foreign<B, P>
Expand All @@ -751,45 +751,45 @@ where
height: Option<u64>,
hash: Option<String>,
commit: Option<String>,
) -> Result<BlockHeaderPrintable, ErrorKind> {
) -> Result<BlockHeaderPrintable, Error> {
let mut parsed_hash: Option<Hash> = None;
if let Some(hash) = hash {
let vec = util::from_hex(&hash)
.map_err(|e| ErrorKind::Argument(format!("invalid block hash: {}", e)))?;
.map_err(|e| Error::Argument(format!("invalid block hash: {}", e)))?;
parsed_hash = Some(Hash::from_vec(&vec));
}
Foreign::get_header(self, height, parsed_hash, commit).map_err(|e| e.kind().clone())
Foreign::get_header(self, height, parsed_hash, commit)
}
fn get_block(
&self,
height: Option<u64>,
hash: Option<String>,
commit: Option<String>,
) -> Result<BlockPrintable, ErrorKind> {
) -> Result<BlockPrintable, Error> {
let mut parsed_hash: Option<Hash> = None;
if let Some(hash) = hash {
let vec = util::from_hex(&hash)
.map_err(|e| ErrorKind::Argument(format!("invalid block hash: {}", e)))?;
.map_err(|e| Error::Argument(format!("invalid block hash: {}", e)))?;
parsed_hash = Some(Hash::from_vec(&vec));
}
Foreign::get_block(self, height, parsed_hash, commit).map_err(|e| e.kind().clone())
Foreign::get_block(self, height, parsed_hash, commit)
}

fn get_version(&self) -> Result<Version, ErrorKind> {
Foreign::get_version(self).map_err(|e| e.kind().clone())
fn get_version(&self) -> Result<Version, Error> {
Foreign::get_version(self)
}

fn get_tip(&self) -> Result<Tip, ErrorKind> {
Foreign::get_tip(self).map_err(|e| e.kind().clone())
fn get_tip(&self) -> Result<Tip, Error> {
Foreign::get_tip(self)
}

fn get_kernel(
&self,
excess: String,
min_height: Option<u64>,
max_height: Option<u64>,
) -> Result<LocatedTxKernel, ErrorKind> {
Foreign::get_kernel(self, excess, min_height, max_height).map_err(|e| e.kind().clone())
) -> Result<LocatedTxKernel, Error> {
Foreign::get_kernel(self, excess, min_height, max_height)
}

fn get_outputs(
Expand All @@ -799,7 +799,7 @@ where
end_height: Option<u64>,
include_proof: Option<bool>,
include_merkle_proof: Option<bool>,
) -> Result<Vec<OutputPrintable>, ErrorKind> {
) -> Result<Vec<OutputPrintable>, Error> {
Foreign::get_outputs(
self,
commits,
Expand All @@ -808,7 +808,6 @@ where
include_proof,
include_merkle_proof,
)
.map_err(|e| e.kind().clone())
}

fn get_unspent_outputs(
Expand All @@ -817,33 +816,31 @@ where
end_index: Option<u64>,
max: u64,
include_proof: Option<bool>,
) -> Result<OutputListing, ErrorKind> {
) -> Result<OutputListing, Error> {
Foreign::get_unspent_outputs(self, start_index, end_index, max, include_proof)
.map_err(|e| e.kind().clone())
}

fn get_pmmr_indices(
&self,
start_block_height: u64,
end_block_height: Option<u64>,
) -> Result<OutputListing, ErrorKind> {
) -> Result<OutputListing, Error> {
Foreign::get_pmmr_indices(self, start_block_height, end_block_height)
.map_err(|e| e.kind().clone())
}

fn get_pool_size(&self) -> Result<usize, ErrorKind> {
Foreign::get_pool_size(self).map_err(|e| e.kind().clone())
fn get_pool_size(&self) -> Result<usize, Error> {
Foreign::get_pool_size(self)
}

fn get_stempool_size(&self) -> Result<usize, ErrorKind> {
Foreign::get_stempool_size(self).map_err(|e| e.kind().clone())
fn get_stempool_size(&self) -> Result<usize, Error> {
Foreign::get_stempool_size(self)
}

fn get_unconfirmed_transactions(&self) -> Result<Vec<PoolEntry>, ErrorKind> {
Foreign::get_unconfirmed_transactions(self).map_err(|e| e.kind().clone())
fn get_unconfirmed_transactions(&self) -> Result<Vec<PoolEntry>, Error> {
Foreign::get_unconfirmed_transactions(self)
}
fn push_transaction(&self, tx: Transaction, fluff: Option<bool>) -> Result<(), ErrorKind> {
Foreign::push_transaction(self, tx, fluff).map_err(|e| e.kind().clone())
fn push_transaction(&self, tx: Transaction, fluff: Option<bool>) -> Result<(), Error> {
Foreign::push_transaction(self, tx, fluff)
}
}

Expand All @@ -854,7 +851,7 @@ macro_rules! doctest_helper_json_rpc_foreign_assert_response {
// create temporary grin server, run jsonrpc request on node api, delete server, return
// json response.

{
{
/*use grin_servers::test_framework::framework::run_doctest;
use grin_util as util;
use serde_json;
Expand Down Expand Up @@ -888,6 +885,6 @@ macro_rules! doctest_helper_json_rpc_foreign_assert_response {
serde_json::to_string_pretty(&expected_response).unwrap()
);
}*/
}
}
};
}
Loading

0 comments on commit a14a8e3

Please sign in to comment.