Skip to content

Commit 3e14178

Browse files
authored
[cli] Improve the error display from server and command errors, eliminate panics (#476)
* Remove didkey and didcomm * Propagate errors back to runtime * Complete error propagation
1 parent 5d4cae9 commit 3e14178

File tree

19 files changed

+272
-811
lines changed

19 files changed

+272
-811
lines changed

cli/src/main.rs

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ use parser::template;
1414
use prost::{DecodeError, Message};
1515
use serde_json::Value;
1616
use services::config::DefaultConfig;
17-
use std::fmt::Display;
1817

1918
pub static mut DEBUG: bool = false;
2019

@@ -55,18 +54,44 @@ fn main() {
5554
let config = DefaultConfig::from(&matches);
5655
let service = parser::parse(&matches);
5756

58-
match services::execute(&service, config) {
59-
Ok(_) => {}
60-
Err(err) => match err {
61-
services::config::Error::IOError => println!("{}", format!("io error").red()),
62-
services::config::Error::SerializationError => {
63-
println!("{}", format!("serialization error").red())
64-
}
65-
services::config::Error::UnknownCommand => unimplemented!("should not be hit"),
66-
Error::APIError(grpc_status) => {
67-
println!("api error: {}", format!("{}", grpc_status).red())
68-
}
57+
match service {
58+
Ok(service) => match services::execute(&service, config) {
59+
Ok(_) => {}
60+
Err(err) => match err {
61+
Error::IOError => println!("{}", format!("io error").red()),
62+
Error::SerializationError => {
63+
println!("{}", format!("serialization error").red())
64+
}
65+
Error::UnknownCommand => unimplemented!("should not be hit"),
66+
Error::APIError { code, message } => {
67+
println!(
68+
"{}: {}: {}",
69+
format!("error").red().bold(),
70+
format!("{}", code.to_lowercase()).bold(),
71+
format!("{}", message.to_lowercase())
72+
);
73+
}
74+
Error::MissingArguments => todo!(),
75+
},
6976
},
77+
Err(err) => {
78+
println!(
79+
"{}: {}: {}",
80+
format!("error").red().bold(),
81+
format!("command error").bold(),
82+
format!("{}", err)
83+
);
84+
println!();
85+
println!(
86+
"{}",
87+
format!(
88+
"For more information try {} or {}",
89+
format!("-h").green(),
90+
format!("--help").green()
91+
)
92+
.italic()
93+
)
94+
}
7095
}
7196
}
7297

cli/src/parser/account.rs

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,38 @@
11
use clap::ArgMatches;
22

3-
pub fn parse<'a>(args: &'a ArgMatches<'_>) -> Command<'a> {
3+
use crate::services::config::Error;
4+
5+
pub(crate) fn parse<'a>(args: &'a ArgMatches<'_>) -> Result<Command<'a>, Error> {
46
if args.is_present("login") {
5-
return sign_in(
7+
sign_in(
68
&args
79
.subcommand_matches("login")
810
.expect("Error parsing request"),
9-
);
11+
)
1012
} else if args.is_present("info") {
11-
return info(
13+
info(
1214
&args
1315
.subcommand_matches("info")
1416
.expect("Error parsing request"),
15-
);
17+
)
1618
} else {
17-
panic!("Unrecognized command")
19+
Err(Error::MissingArguments)
1820
}
1921
}
2022

21-
fn sign_in<'a>(args: &'a ArgMatches<'_>) -> Command<'a> {
22-
Command::SignIn(SignInArgs {
23+
fn sign_in<'a>(args: &'a ArgMatches<'_>) -> Result<Command<'a>, Error> {
24+
Ok(Command::SignIn(SignInArgs {
2325
name: args.value_of("name"),
2426
email: args.value_of("email"),
2527
sms: args.value_of("sms"),
2628
invitation_code: args.value_of("invitation-code"),
2729
alias: args.value_of("alias"),
2830
set_default: args.is_present("default"),
29-
})
31+
}))
3032
}
3133

32-
fn info<'a>(_args: &'a ArgMatches<'_>) -> Command<'a> {
33-
Command::Info(InfoArgs {})
34+
fn info<'a>(_args: &'a ArgMatches<'_>) -> Result<Command<'a>, Error> {
35+
Ok(Command::Info(InfoArgs {}))
3436
}
3537

3638
#[derive(Debug, PartialEq)]

cli/src/parser/didcomm.rs

Lines changed: 0 additions & 105 deletions
This file was deleted.

cli/src/parser/didkey.rs

Lines changed: 0 additions & 49 deletions
This file was deleted.

cli/src/parser/issuer.rs

Lines changed: 25 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1+
use crate::services::config::Error;
12
use clap::ArgMatches;
2-
use colored::Colorize;
33

4-
pub(crate) fn parse<'a>(args: &'a ArgMatches<'_>) -> Command<'a> {
4+
pub(crate) fn parse<'a>(args: &'a ArgMatches<'_>) -> Result<Command<'a>, Error> {
55
if args.is_present("issue") {
66
issue(
77
&args
@@ -27,72 +27,68 @@ pub(crate) fn parse<'a>(args: &'a ArgMatches<'_>) -> Command<'a> {
2727
.expect("Error parsing request"),
2828
)
2929
} else if args.is_present("create-proof") {
30-
return create_proof(
30+
create_proof(
3131
&args
3232
.subcommand_matches("create-proof")
3333
.expect("Error parsing request"),
34-
);
34+
)
3535
} else if args.is_present("verify-proof") {
36-
return verify_proof(
36+
verify_proof(
3737
&args
3838
.subcommand_matches("verify-proof")
3939
.expect("Error parsing request"),
40-
);
40+
)
4141
} else {
42-
println!(
43-
"{}",
44-
format!("invalid subcommand. see 'trinsic vc -h' for details").red()
45-
);
46-
panic!();
42+
Err(Error::MissingArguments)
4743
}
4844
}
4945

50-
fn issue<'a>(args: &'a ArgMatches<'_>) -> Command<'a> {
51-
Command::Issue(IssueArgs {
46+
fn issue<'a>(args: &'a ArgMatches<'_>) -> Result<Command<'a>, Error> {
47+
Ok(Command::Issue(IssueArgs {
5248
document: args.value_of("document"),
5349
out: args.value_of("out"),
54-
})
50+
}))
5551
}
5652

57-
fn issue_from_template<'a>(args: &'a ArgMatches<'_>) -> Command<'a> {
58-
Command::IssueFromTemplate(IssueFromTemplateArgs {
53+
fn issue_from_template<'a>(args: &'a ArgMatches<'_>) -> Result<Command<'a>, Error> {
54+
Ok(Command::IssueFromTemplate(IssueFromTemplateArgs {
5955
template_id: args
6056
.value_of("template-id")
6157
.map_or(String::default(), |x| x.to_string()),
6258
values_json: args.value_of("values-data").map(|x| x.to_string()),
6359
values_file: args.value_of("values-file").map(|x| x.to_string()),
64-
})
60+
}))
6561
}
6662

67-
fn get_status<'a>(args: &'a ArgMatches<'_>) -> Command<'a> {
68-
Command::GetStatus(GetStatusArgs {
63+
fn get_status<'a>(args: &'a ArgMatches<'_>) -> Result<Command<'a>, Error> {
64+
Ok(Command::GetStatus(GetStatusArgs {
6965
credential_status_id: args
7066
.value_of("credential-status-id")
7167
.map_or(String::default(), |x| x.to_string()),
72-
})
68+
}))
7369
}
7470

75-
fn update_status<'a>(args: &'a ArgMatches<'_>) -> Command<'a> {
76-
Command::UpdateStatus(UpdateStatusArgs {
71+
fn update_status<'a>(args: &'a ArgMatches<'_>) -> Result<Command<'a>, Error> {
72+
Ok(Command::UpdateStatus(UpdateStatusArgs {
7773
credential_status_id: args
7874
.value_of("credential-status-id")
7975
.map_or(String::default(), |x| x.to_string()),
8076
revoked: args.is_present("revoked"),
81-
})
77+
}))
8278
}
8379

84-
fn create_proof<'a>(args: &'a ArgMatches<'_>) -> Command<'a> {
85-
Command::CreateProof(CreateProofArgs {
80+
fn create_proof<'a>(args: &'a ArgMatches<'_>) -> Result<Command<'a>, Error> {
81+
Ok(Command::CreateProof(CreateProofArgs {
8682
reveal_document: args.value_of("reveal-document"),
8783
document_id: args.value_of("document-id"),
8884
out: args.value_of("out"),
89-
})
85+
}))
9086
}
9187

92-
fn verify_proof<'a>(args: &'a ArgMatches<'_>) -> Command<'a> {
93-
Command::VerifyProof(VerifyProofArgs {
88+
fn verify_proof<'a>(args: &'a ArgMatches<'_>) -> Result<Command<'a>, Error> {
89+
Ok(Command::VerifyProof(VerifyProofArgs {
9490
proof_document: args.value_of("proof-document"),
95-
})
91+
}))
9692
}
9793

9894
#[derive(Debug, PartialEq)]

0 commit comments

Comments
 (0)