Skip to content

Commit a14a8e3

Browse files
yeastplumetrevyn
andauthored
Thiserror changeover (#3728)
* 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 <[email protected]>
1 parent 03b007c commit a14a8e3

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

75 files changed

+1780
-2194
lines changed

Cargo.lock

+270-257
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

-2
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,6 @@ futures = "0.3.19"
3131
serde_json = "1"
3232
log = "0.4"
3333
term = "0.6"
34-
failure = "0.1"
35-
failure_derive = "0.1"
3634

3735
grin_api = { path = "./api", version = "5.2.0-alpha.1" }
3836
grin_config = { path = "./config", version = "5.2.0-alpha.1" }

api/Cargo.toml

+1-2
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,14 @@ edition = "2018"
1111

1212
[dependencies]
1313
easy-jsonrpc-mw = "0.5.4"
14-
failure = "0.1.1"
15-
failure_derive = "0.1.1"
1614
hyper = "0.13"
1715
lazy_static = "1"
1816
regex = "1"
1917
ring = "0.16"
2018
serde = "1"
2119
serde_derive = "1"
2220
serde_json = "1"
21+
thiserror = "1"
2322
log = "0.4"
2423
tokio = { version = "0.2", features = ["full"] }
2524
tokio-rustls = "0.13"

api/src/client.rs

+11-17
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,8 @@
1414

1515
//! High level JSON/HTTP client API
1616
17-
use crate::rest::{Error, ErrorKind};
17+
use crate::rest::Error;
1818
use crate::util::to_base64;
19-
use failure::{Fail, ResultExt};
2019
use hyper::body;
2120
use hyper::header::{ACCEPT, AUTHORIZATION, CONTENT_TYPE, USER_AGENT};
2221
use hyper::{Body, Client, Request};
@@ -181,9 +180,7 @@ fn build_request(
181180
None => Body::empty(),
182181
Some(json) => json.into(),
183182
})
184-
.map_err(|e| {
185-
ErrorKind::RequestError(format!("Bad request {} {}: {}", method, url, e)).into()
186-
})
183+
.map_err(|e| Error::RequestError(format!("Bad request {} {}: {}", method, url, e)))
187184
}
188185

189186
pub fn create_post_request<IN>(
@@ -194,9 +191,8 @@ pub fn create_post_request<IN>(
194191
where
195192
IN: Serialize,
196193
{
197-
let json = serde_json::to_string(input).context(ErrorKind::Internal(
198-
"Could not serialize data to JSON".to_owned(),
199-
))?;
194+
let json = serde_json::to_string(input)
195+
.map_err(|e| Error::Internal(format!("Could not serialize data to JSON: {}", e)))?;
200196
build_request(url, "POST", api_secret, Some(json))
201197
}
202198

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

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

@@ -237,10 +231,10 @@ async fn send_request_async(req: Request<Body>, timeout: TimeOut) -> Result<Stri
237231
let resp = client
238232
.request(req)
239233
.await
240-
.map_err(|e| ErrorKind::RequestError(format!("Cannot make request: {}", e)))?;
234+
.map_err(|e| Error::RequestError(format!("Cannot make request: {}", e)))?;
241235

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

251245
let raw = body::to_bytes(resp)
252246
.await
253-
.map_err(|e| ErrorKind::RequestError(format!("Cannot read response body: {}", e)))?;
247+
.map_err(|e| Error::RequestError(format!("Cannot read response body: {}", e)))?;
254248

255249
Ok(String::from_utf8_lossy(&raw).to_string())
256250
}
@@ -260,6 +254,6 @@ pub fn send_request(req: Request<Body>, timeout: TimeOut) -> Result<String, Erro
260254
.basic_scheduler()
261255
.enable_all()
262256
.build()
263-
.map_err(|e| ErrorKind::RequestError(format!("{}", e)))?;
257+
.map_err(|e| Error::RequestError(format!("{}", e)))?;
264258
rt.block_on(send_request_async(req, timeout))
265259
}

api/src/foreign_rpc.rs

+38-41
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use crate::core::core::transaction::Transaction;
1919
use crate::foreign::Foreign;
2020
use crate::pool::PoolEntry;
2121
use crate::pool::{BlockChain, PoolAdapter};
22-
use crate::rest::ErrorKind;
22+
use crate::rest::Error;
2323
use crate::types::{
2424
BlockHeaderPrintable, BlockPrintable, LocatedTxKernel, OutputListing, OutputPrintable, Tip,
2525
Version,
@@ -126,7 +126,7 @@ pub trait ForeignRpc: Sync + Send {
126126
height: Option<u64>,
127127
hash: Option<String>,
128128
commit: Option<String>,
129-
) -> Result<BlockHeaderPrintable, ErrorKind>;
129+
) -> Result<BlockHeaderPrintable, Error>;
130130

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

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

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

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

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

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

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

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

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

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

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

744744
impl<B, P> ForeignRpc for Foreign<B, P>
@@ -751,45 +751,45 @@ where
751751
height: Option<u64>,
752752
hash: Option<String>,
753753
commit: Option<String>,
754-
) -> Result<BlockHeaderPrintable, ErrorKind> {
754+
) -> Result<BlockHeaderPrintable, Error> {
755755
let mut parsed_hash: Option<Hash> = None;
756756
if let Some(hash) = hash {
757757
let vec = util::from_hex(&hash)
758-
.map_err(|e| ErrorKind::Argument(format!("invalid block hash: {}", e)))?;
758+
.map_err(|e| Error::Argument(format!("invalid block hash: {}", e)))?;
759759
parsed_hash = Some(Hash::from_vec(&vec));
760760
}
761-
Foreign::get_header(self, height, parsed_hash, commit).map_err(|e| e.kind().clone())
761+
Foreign::get_header(self, height, parsed_hash, commit)
762762
}
763763
fn get_block(
764764
&self,
765765
height: Option<u64>,
766766
hash: Option<String>,
767767
commit: Option<String>,
768-
) -> Result<BlockPrintable, ErrorKind> {
768+
) -> Result<BlockPrintable, Error> {
769769
let mut parsed_hash: Option<Hash> = None;
770770
if let Some(hash) = hash {
771771
let vec = util::from_hex(&hash)
772-
.map_err(|e| ErrorKind::Argument(format!("invalid block hash: {}", e)))?;
772+
.map_err(|e| Error::Argument(format!("invalid block hash: {}", e)))?;
773773
parsed_hash = Some(Hash::from_vec(&vec));
774774
}
775-
Foreign::get_block(self, height, parsed_hash, commit).map_err(|e| e.kind().clone())
775+
Foreign::get_block(self, height, parsed_hash, commit)
776776
}
777777

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

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

786786
fn get_kernel(
787787
&self,
788788
excess: String,
789789
min_height: Option<u64>,
790790
max_height: Option<u64>,
791-
) -> Result<LocatedTxKernel, ErrorKind> {
792-
Foreign::get_kernel(self, excess, min_height, max_height).map_err(|e| e.kind().clone())
791+
) -> Result<LocatedTxKernel, Error> {
792+
Foreign::get_kernel(self, excess, min_height, max_height)
793793
}
794794

795795
fn get_outputs(
@@ -799,7 +799,7 @@ where
799799
end_height: Option<u64>,
800800
include_proof: Option<bool>,
801801
include_merkle_proof: Option<bool>,
802-
) -> Result<Vec<OutputPrintable>, ErrorKind> {
802+
) -> Result<Vec<OutputPrintable>, Error> {
803803
Foreign::get_outputs(
804804
self,
805805
commits,
@@ -808,7 +808,6 @@ where
808808
include_proof,
809809
include_merkle_proof,
810810
)
811-
.map_err(|e| e.kind().clone())
812811
}
813812

814813
fn get_unspent_outputs(
@@ -817,33 +816,31 @@ where
817816
end_index: Option<u64>,
818817
max: u64,
819818
include_proof: Option<bool>,
820-
) -> Result<OutputListing, ErrorKind> {
819+
) -> Result<OutputListing, Error> {
821820
Foreign::get_unspent_outputs(self, start_index, end_index, max, include_proof)
822-
.map_err(|e| e.kind().clone())
823821
}
824822

825823
fn get_pmmr_indices(
826824
&self,
827825
start_block_height: u64,
828826
end_block_height: Option<u64>,
829-
) -> Result<OutputListing, ErrorKind> {
827+
) -> Result<OutputListing, Error> {
830828
Foreign::get_pmmr_indices(self, start_block_height, end_block_height)
831-
.map_err(|e| e.kind().clone())
832829
}
833830

834-
fn get_pool_size(&self) -> Result<usize, ErrorKind> {
835-
Foreign::get_pool_size(self).map_err(|e| e.kind().clone())
831+
fn get_pool_size(&self) -> Result<usize, Error> {
832+
Foreign::get_pool_size(self)
836833
}
837834

838-
fn get_stempool_size(&self) -> Result<usize, ErrorKind> {
839-
Foreign::get_stempool_size(self).map_err(|e| e.kind().clone())
835+
fn get_stempool_size(&self) -> Result<usize, Error> {
836+
Foreign::get_stempool_size(self)
840837
}
841838

842-
fn get_unconfirmed_transactions(&self) -> Result<Vec<PoolEntry>, ErrorKind> {
843-
Foreign::get_unconfirmed_transactions(self).map_err(|e| e.kind().clone())
839+
fn get_unconfirmed_transactions(&self) -> Result<Vec<PoolEntry>, Error> {
840+
Foreign::get_unconfirmed_transactions(self)
844841
}
845-
fn push_transaction(&self, tx: Transaction, fluff: Option<bool>) -> Result<(), ErrorKind> {
846-
Foreign::push_transaction(self, tx, fluff).map_err(|e| e.kind().clone())
842+
fn push_transaction(&self, tx: Transaction, fluff: Option<bool>) -> Result<(), Error> {
843+
Foreign::push_transaction(self, tx, fluff)
847844
}
848845
}
849846

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

857-
{
854+
{
858855
/*use grin_servers::test_framework::framework::run_doctest;
859856
use grin_util as util;
860857
use serde_json;
@@ -888,6 +885,6 @@ macro_rules! doctest_helper_json_rpc_foreign_assert_response {
888885
serde_json::to_string_pretty(&expected_response).unwrap()
889886
);
890887
}*/
891-
}
888+
}
892889
};
893890
}

0 commit comments

Comments
 (0)