Skip to content

Commit 5ceb16f

Browse files
committed
first half of error types removed, replaced with report
1 parent d69ce4d commit 5ceb16f

File tree

24 files changed

+167
-252
lines changed

24 files changed

+167
-252
lines changed

Cargo.lock

Lines changed: 60 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

catalyst-toolbox/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ structopt = "0.3"
4848
stderrlog = "0.5"
4949
serde_yaml = "0.8.17"
5050
sscanf = "0.1"
51+
color-eyre = "0.6"
5152
thiserror = "1.0"
5253
tokio = { version = "1.8", features = ["rt", "macros"] }
5354
url = "2.2"
Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,9 @@
1-
use std::error::Error;
2-
3-
use structopt::StructOpt as _;
1+
use structopt::StructOpt;
42

53
pub mod cli;
64

7-
fn main() {
8-
cli::Cli::from_args().exec().unwrap_or_else(report_error)
9-
}
10-
11-
fn report_error(error: Box<dyn Error>) {
12-
eprintln!("{}", error);
13-
let mut source = error.source();
14-
while let Some(sub_error) = source {
15-
eprintln!(" |-> {}", sub_error);
16-
source = sub_error.source();
17-
}
18-
std::process::exit(1)
5+
fn main() -> color_eyre::Result<()> {
6+
color_eyre::install()?;
7+
cli::Cli::from_args().exec()?;
8+
Ok(())
199
}

catalyst-toolbox/src/bin/cli/ideascale/mod.rs

Lines changed: 11 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use catalyst_toolbox::ideascale::{
2-
build_challenges, build_fund, build_proposals, fetch_all, CustomFieldTags,
3-
Error as IdeascaleError, Scores, Sponsors,
2+
build_challenges, build_fund, build_proposals, fetch_all, CustomFieldTags, Scores, Sponsors,
43
};
4+
use color_eyre::Report;
55
use jcli_lib::utils::io as io_utils;
66
use jormungandr_lib::interfaces::VotePrivacy;
77
use std::collections::HashSet;
@@ -12,21 +12,6 @@ use serde::de::DeserializeOwned;
1212
use serde::Serialize;
1313
use std::path::{Path, PathBuf};
1414

15-
#[derive(thiserror::Error, Debug)]
16-
pub enum Error {
17-
#[error(transparent)]
18-
Ideascale(#[from] IdeascaleError),
19-
20-
#[error(transparent)]
21-
Io(#[from] std::io::Error),
22-
23-
#[error(transparent)]
24-
Csv(#[from] csv::Error),
25-
26-
#[error(transparent)]
27-
Serde(#[from] serde_json::Error),
28-
}
29-
3015
#[derive(Debug, StructOpt)]
3116
pub enum Ideascale {
3217
Import(Import),
@@ -89,15 +74,15 @@ pub struct Import {
8974
}
9075

9176
impl Ideascale {
92-
pub fn exec(&self) -> Result<(), Error> {
77+
pub fn exec(&self) -> Result<(), Report> {
9378
match self {
9479
Ideascale::Import(import) => import.exec(),
9580
}
9681
}
9782
}
9883

9984
impl Import {
100-
fn exec(&self) -> Result<(), Error> {
85+
fn exec(&self) -> Result<(), Report> {
10186
let Import {
10287
fund,
10388
fund_goal,
@@ -182,21 +167,22 @@ impl Import {
182167
}
183168
}
184169

185-
fn dump_content_to_file(content: impl Serialize, file_path: &Path) -> Result<(), Error> {
170+
fn dump_content_to_file(content: impl Serialize, file_path: &Path) -> Result<(), Report> {
186171
let writer = jcli_lib::utils::io::open_file_write(&Some(file_path))?;
187-
serde_json::to_writer_pretty(writer, &content).map_err(Error::Serde)
172+
serde_json::to_writer_pretty(writer, &content)?;
173+
Ok(())
188174
}
189175

190-
fn read_json_from_file<T: DeserializeOwned>(file_path: &Path) -> Result<T, Error> {
176+
fn read_json_from_file<T: DeserializeOwned>(file_path: &Path) -> Result<T, Report> {
191177
let reader = io_utils::open_file_read(&Some(file_path))?;
192-
serde_json::from_reader(reader).map_err(Error::Serde)
178+
Ok(serde_json::from_reader(reader)?)
193179
}
194180

195181
fn parse_from_csv(s: &str) -> Filters {
196182
s.split(';').map(|x| x.to_string()).collect()
197183
}
198184

199-
fn read_scores_file(path: &Option<PathBuf>) -> Result<Scores, Error> {
185+
fn read_scores_file(path: &Option<PathBuf>) -> Result<Scores, Report> {
200186
let mut scores = Scores::new();
201187
if let Some(path) = path {
202188
let mut reader = csv::Reader::from_path(path)?;
@@ -218,7 +204,7 @@ fn read_scores_file(path: &Option<PathBuf>) -> Result<Scores, Error> {
218204
Ok(scores)
219205
}
220206

221-
fn read_sponsors_file(path: &Option<PathBuf>) -> Result<Sponsors, Error> {
207+
fn read_sponsors_file(path: &Option<PathBuf>) -> Result<Sponsors, Report> {
222208
let mut sponsors = Sponsors::new();
223209

224210
if let Some(path) = path {

catalyst-toolbox/src/bin/cli/kedqr/decode/img.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use catalyst_toolbox::kedqr::QrPin;
44
use chain_crypto::AsymmetricKey;
55
use chain_crypto::Ed25519Extended;
66
use chain_crypto::SecretKey;
7-
use std::error::Error;
7+
use color_eyre::Report;
88
use std::fs::File;
99
use std::io::Write;
1010
use std::path::Path;
@@ -14,7 +14,7 @@ pub fn save_secret_from_qr(
1414
qr: PathBuf,
1515
output: Option<PathBuf>,
1616
pin: QrPin,
17-
) -> Result<(), Box<dyn Error>> {
17+
) -> Result<(), Report> {
1818
let sk = secret_from_qr(&qr, pin)?;
1919
let hrp = Ed25519Extended::SECRET_BECH32_HRP;
2020
let secret_key = bech32::encode(hrp, sk.leak_secret().to_base32(), Variant::Bech32)?;
@@ -36,7 +36,7 @@ pub fn save_secret_from_qr(
3636
pub fn secret_from_qr(
3737
qr: impl AsRef<Path>,
3838
pin: QrPin,
39-
) -> Result<SecretKey<Ed25519Extended>, catalyst_toolbox::kedqr::KeyQrCodeError> {
39+
) -> Result<SecretKey<Ed25519Extended>, Report> {
4040
let img = image::open(qr)?;
4141
let secret = KeyQrCode::decode(img, &pin.password)?;
4242
Ok(secret.first().unwrap().clone())

catalyst-toolbox/src/bin/cli/kedqr/decode/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ mod payload;
33

44
use crate::cli::kedqr::QrCodeOpts;
55
use catalyst_toolbox::kedqr::QrPin;
6+
use color_eyre::Report;
67
pub use img::{save_secret_from_qr, secret_from_qr};
78
pub use payload::{decode_payload, secret_from_payload};
8-
use std::error::Error;
99
use std::path::PathBuf;
1010
use structopt::StructOpt;
1111

@@ -28,7 +28,7 @@ pub struct DecodeQrCodeCmd {
2828
}
2929

3030
impl DecodeQrCodeCmd {
31-
pub fn exec(self) -> Result<(), Box<dyn Error>> {
31+
pub fn exec(self) -> Result<(), Report> {
3232
match self.opts {
3333
QrCodeOpts::Payload => decode_payload(self.input, self.output, self.pin),
3434
QrCodeOpts::Img => save_secret_from_qr(self.input, self.output, self.pin),

catalyst-toolbox/src/bin/cli/kedqr/decode/payload.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use bech32::{ToBase32, Variant};
22
use catalyst_toolbox::kedqr::{decode, QrPin};
33
use chain_crypto::{AsymmetricKey, Ed25519Extended, SecretKey};
4+
use color_eyre::Report;
45
use std::{
5-
error::Error,
66
fs::{File, OpenOptions},
77
io::{BufRead, BufReader, Write},
88
path::{Path, PathBuf},
@@ -12,7 +12,7 @@ pub fn decode_payload(
1212
input: PathBuf,
1313
output: Option<PathBuf>,
1414
pin: QrPin,
15-
) -> Result<(), Box<dyn Error>> {
15+
) -> Result<(), Report> {
1616
// generate qrcode with key and parsed pin
1717
let secret = secret_from_payload(input, pin)?;
1818
let hrp = Ed25519Extended::SECRET_BECH32_HRP;
@@ -35,7 +35,7 @@ pub fn decode_payload(
3535
pub fn secret_from_payload(
3636
input: impl AsRef<Path>,
3737
pin: QrPin,
38-
) -> Result<SecretKey<Ed25519Extended>, catalyst_toolbox::kedqr::KeyQrCodePayloadError> {
38+
) -> Result<SecretKey<Ed25519Extended>, Report> {
3939
let input = OpenOptions::new()
4040
.create(false)
4141
.read(true)

catalyst-toolbox/src/bin/cli/kedqr/encode/img.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use catalyst_toolbox::kedqr::{KeyQrCode, QrPin};
22
use chain_crypto::bech32::Bech32;
33
use chain_crypto::{Ed25519Extended, SecretKey};
4+
use color_eyre::Report;
45
use std::{
5-
error::Error,
66
fs::OpenOptions,
77
io::{BufRead, BufReader},
88
path::PathBuf,
@@ -12,7 +12,7 @@ pub fn generate_qr(
1212
input: PathBuf,
1313
output: Option<PathBuf>,
1414
pin: QrPin,
15-
) -> Result<(), Box<dyn Error>> {
15+
) -> Result<(), Report> {
1616
// open input key and parse it
1717
let key_file = OpenOptions::new()
1818
.create(false)

catalyst-toolbox/src/bin/cli/kedqr/encode/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ mod payload;
33

44
use crate::cli::kedqr::QrCodeOpts;
55
use catalyst_toolbox::kedqr::QrPin;
6+
use color_eyre::Report;
67
pub use img::generate_qr;
78
pub use payload::generate_payload;
8-
use std::error::Error;
99
use std::path::PathBuf;
1010
use structopt::StructOpt;
1111

@@ -28,7 +28,7 @@ pub struct EncodeQrCodeCmd {
2828
}
2929

3030
impl EncodeQrCodeCmd {
31-
pub fn exec(self) -> Result<(), Box<dyn Error>> {
31+
pub fn exec(self) -> Result<(), Report> {
3232
match self.opts {
3333
QrCodeOpts::Payload => generate_payload(self.input, self.output, self.pin),
3434
QrCodeOpts::Img => generate_qr(self.input, self.output, self.pin),

catalyst-toolbox/src/bin/cli/kedqr/encode/payload.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ use catalyst_toolbox::kedqr::generate;
22
use catalyst_toolbox::kedqr::QrPin;
33
use chain_crypto::bech32::Bech32;
44
use chain_crypto::{Ed25519Extended, SecretKey};
5+
use color_eyre::Report;
56
use std::fs::File;
67
use std::io::Write;
78
use std::{
8-
error::Error,
99
fs::OpenOptions,
1010
io::{BufRead, BufReader},
1111
path::PathBuf,
@@ -15,7 +15,7 @@ pub fn generate_payload(
1515
input: PathBuf,
1616
output: Option<PathBuf>,
1717
pin: QrPin,
18-
) -> Result<(), Box<dyn Error>> {
18+
) -> Result<(), Report> {
1919
// open input key and parse it
2020
let key_file = OpenOptions::new()
2121
.create(false)

0 commit comments

Comments
 (0)