Skip to content

Commit eebc3b5

Browse files
committed
refactor(error): Make 'Error' less churn heavy
When I'm making changes, I frequently have to touch every error function. This creates a more standard builder API so we can more easily add or modify fields without having to update every case.
1 parent 4a6430c commit eebc3b5

File tree

1 file changed

+46
-152
lines changed

1 file changed

+46
-152
lines changed

src/parse/errors.rs

Lines changed: 46 additions & 152 deletions
Original file line numberDiff line numberDiff line change
@@ -544,6 +544,16 @@ impl Error {
544544
}
545545
}
546546

547+
pub(crate) fn set_info(mut self, info: Vec<String>) -> Self {
548+
self.info = info;
549+
self
550+
}
551+
552+
pub(crate) fn set_source(mut self, source: Box<dyn error::Error + Send + Sync>) -> Self {
553+
self.source = Some(source);
554+
self
555+
}
556+
547557
pub(crate) fn user_error(
548558
app: &App,
549559
usage: String,
@@ -556,13 +566,7 @@ impl Error {
556566
put_usage(&mut c, usage);
557567
try_help(app, &mut c);
558568

559-
Self {
560-
message: c,
561-
kind,
562-
info: vec![],
563-
source: None,
564-
backtrace: Backtrace::new(),
565-
}
569+
Self::new(c, kind)
566570
}
567571

568572
pub(crate) fn argument_conflict(
@@ -597,13 +601,7 @@ impl Error {
597601
info.push(other);
598602
}
599603

600-
Error {
601-
message: c,
602-
kind: ErrorKind::ArgumentConflict,
603-
info,
604-
source: None,
605-
backtrace: Backtrace::new(),
606-
}
604+
Self::new(c, ErrorKind::ArgumentConflict).set_info(info)
607605
}
608606

609607
pub(crate) fn empty_value(app: &App, arg: &Arg, usage: String) -> Self {
@@ -616,13 +614,7 @@ impl Error {
616614
put_usage(&mut c, usage);
617615
try_help(app, &mut c);
618616

619-
Error {
620-
message: c,
621-
kind: ErrorKind::EmptyValue,
622-
info: vec![arg],
623-
source: None,
624-
backtrace: Backtrace::new(),
625-
}
617+
Self::new(c, ErrorKind::EmptyValue).set_info(vec![arg])
626618
}
627619

628620
pub(crate) fn no_equals(app: &App, arg: String, usage: String) -> Self {
@@ -635,13 +627,7 @@ impl Error {
635627
put_usage(&mut c, usage);
636628
try_help(app, &mut c);
637629

638-
Error {
639-
message: c,
640-
kind: ErrorKind::NoEquals,
641-
info: vec![arg],
642-
source: None,
643-
backtrace: Backtrace::new(),
644-
}
630+
Self::new(c, ErrorKind::NoEquals).set_info(vec![arg])
645631
}
646632

647633
pub(crate) fn invalid_value<G>(
@@ -699,13 +685,7 @@ impl Error {
699685
let mut info = vec![arg.to_string(), bad_val];
700686
info.extend(sorted);
701687

702-
Error {
703-
message: c,
704-
kind: ErrorKind::InvalidValue,
705-
info,
706-
source: None,
707-
backtrace: Backtrace::new(),
708-
}
688+
Self::new(c, ErrorKind::InvalidValue).set_info(info)
709689
}
710690

711691
pub(crate) fn invalid_subcommand(
@@ -731,13 +711,7 @@ impl Error {
731711
put_usage(&mut c, usage);
732712
try_help(app, &mut c);
733713

734-
Error {
735-
message: c,
736-
kind: ErrorKind::InvalidSubcommand,
737-
info: vec![subcmd],
738-
source: None,
739-
backtrace: Backtrace::new(),
740-
}
714+
Self::new(c, ErrorKind::InvalidSubcommand).set_info(vec![subcmd])
741715
}
742716

743717
pub(crate) fn unrecognized_subcommand(app: &App, subcmd: String, name: String) -> Self {
@@ -750,13 +724,7 @@ impl Error {
750724
c.none(format!("\n {} <subcommands>", name));
751725
try_help(app, &mut c);
752726

753-
Error {
754-
message: c,
755-
kind: ErrorKind::UnrecognizedSubcommand,
756-
info: vec![subcmd],
757-
source: None,
758-
backtrace: Backtrace::new(),
759-
}
727+
Self::new(c, ErrorKind::UnrecognizedSubcommand).set_info(vec![subcmd])
760728
}
761729

762730
pub(crate) fn missing_required_argument(
@@ -781,13 +749,7 @@ impl Error {
781749
put_usage(&mut c, usage);
782750
try_help(app, &mut c);
783751

784-
Error {
785-
message: c,
786-
kind: ErrorKind::MissingRequiredArgument,
787-
info,
788-
source: None,
789-
backtrace: Backtrace::new(),
790-
}
752+
Self::new(c, ErrorKind::MissingRequiredArgument).set_info(info)
791753
}
792754

793755
pub(crate) fn missing_subcommand(app: &App, name: String, usage: String) -> Self {
@@ -799,13 +761,7 @@ impl Error {
799761
put_usage(&mut c, usage);
800762
try_help(app, &mut c);
801763

802-
Error {
803-
message: c,
804-
kind: ErrorKind::MissingSubcommand,
805-
info: vec![],
806-
source: None,
807-
backtrace: Backtrace::new(),
808-
}
764+
Self::new(c, ErrorKind::MissingSubcommand)
809765
}
810766

811767
pub(crate) fn invalid_utf8(app: &App, usage: String) -> Self {
@@ -818,13 +774,7 @@ impl Error {
818774
put_usage(&mut c, usage);
819775
try_help(app, &mut c);
820776

821-
Error {
822-
message: c,
823-
kind: ErrorKind::InvalidUtf8,
824-
info: vec![],
825-
source: None,
826-
backtrace: Backtrace::new(),
827-
}
777+
Self::new(c, ErrorKind::InvalidUtf8)
828778
}
829779

830780
pub(crate) fn too_many_occurrences(
@@ -847,17 +797,11 @@ impl Error {
847797
put_usage(&mut c, usage);
848798
try_help(app, &mut c);
849799

850-
Error {
851-
message: c,
852-
kind: ErrorKind::TooManyOccurrences,
853-
info: vec![
854-
arg.to_string(),
855-
curr_occurs.to_string(),
856-
max_occurs.to_string(),
857-
],
858-
source: None,
859-
backtrace: Backtrace::new(),
860-
}
800+
Self::new(c, ErrorKind::TooManyOccurrences).set_info(vec![
801+
arg.to_string(),
802+
curr_occurs.to_string(),
803+
max_occurs.to_string(),
804+
])
861805
}
862806

863807
pub(crate) fn too_many_values(app: &App, val: String, arg: String, usage: String) -> Self {
@@ -871,13 +815,7 @@ impl Error {
871815
put_usage(&mut c, usage);
872816
try_help(app, &mut c);
873817

874-
Error {
875-
message: c,
876-
kind: ErrorKind::TooManyValues,
877-
info: vec![arg, val],
878-
source: None,
879-
backtrace: Backtrace::new(),
880-
}
818+
Self::new(c, ErrorKind::TooManyValues).set_info(vec![arg, val])
881819
}
882820

883821
pub(crate) fn too_few_values(
@@ -900,13 +838,11 @@ impl Error {
900838
put_usage(&mut c, usage);
901839
try_help(app, &mut c);
902840

903-
Error {
904-
message: c,
905-
kind: ErrorKind::TooFewValues,
906-
info: vec![arg.to_string(), curr_vals.to_string(), min_vals.to_string()],
907-
source: None,
908-
backtrace: Backtrace::new(),
909-
}
841+
Self::new(c, ErrorKind::TooFewValues).set_info(vec![
842+
arg.to_string(),
843+
curr_vals.to_string(),
844+
min_vals.to_string(),
845+
])
910846
}
911847

912848
pub(crate) fn value_validation(
@@ -915,21 +851,9 @@ impl Error {
915851
val: String,
916852
err: Box<dyn error::Error + Send + Sync>,
917853
) -> Self {
918-
let Self {
919-
mut message,
920-
kind,
921-
info,
922-
source,
923-
backtrace,
924-
} = Self::value_validation_with_color(arg, val, err, app.get_color());
925-
try_help(app, &mut message);
926-
Self {
927-
message,
928-
kind,
929-
info,
930-
source,
931-
backtrace,
932-
}
854+
let mut err = Self::value_validation_with_color(arg, val, err, app.get_color());
855+
try_help(app, &mut err.message);
856+
err
933857
}
934858

935859
pub(crate) fn value_validation_without_app(
@@ -956,13 +880,9 @@ impl Error {
956880

957881
c.none(format!(": {}", err));
958882

959-
Error {
960-
message: c,
961-
kind: ErrorKind::ValueValidation,
962-
info: vec![arg, val, err.to_string()],
963-
source: Some(err),
964-
backtrace: Backtrace::new(),
965-
}
883+
Self::new(c, ErrorKind::ValueValidation)
884+
.set_info(vec![arg, val, err.to_string()])
885+
.set_source(err)
966886
}
967887

968888
pub(crate) fn wrong_number_of_values(
@@ -985,13 +905,11 @@ impl Error {
985905
put_usage(&mut c, usage);
986906
try_help(app, &mut c);
987907

988-
Error {
989-
message: c,
990-
kind: ErrorKind::WrongNumberOfValues,
991-
info: vec![arg.to_string(), curr_vals.to_string(), num_vals.to_string()],
992-
source: None,
993-
backtrace: Backtrace::new(),
994-
}
908+
Self::new(c, ErrorKind::WrongNumberOfValues).set_info(vec![
909+
arg.to_string(),
910+
curr_vals.to_string(),
911+
num_vals.to_string(),
912+
])
995913
}
996914

997915
pub(crate) fn unexpected_multiple_usage(app: &App, arg: &Arg, usage: String) -> Self {
@@ -1004,13 +922,7 @@ impl Error {
1004922
put_usage(&mut c, usage);
1005923
try_help(app, &mut c);
1006924

1007-
Error {
1008-
message: c,
1009-
kind: ErrorKind::UnexpectedMultipleUsage,
1010-
info: vec![arg],
1011-
source: None,
1012-
backtrace: Backtrace::new(),
1013-
}
925+
Self::new(c, ErrorKind::UnexpectedMultipleUsage).set_info(vec![arg])
1014926
}
1015927

1016928
pub(crate) fn unknown_argument(
@@ -1054,13 +966,7 @@ impl Error {
1054966
put_usage(&mut c, usage);
1055967
try_help(app, &mut c);
1056968

1057-
Error {
1058-
message: c,
1059-
kind: ErrorKind::UnknownArgument,
1060-
info: vec![arg],
1061-
source: None,
1062-
backtrace: Backtrace::new(),
1063-
}
969+
Self::new(c, ErrorKind::UnknownArgument).set_info(vec![arg])
1064970
}
1065971

1066972
pub(crate) fn unnecessary_double_dash(app: &App, arg: String, usage: String) -> Self {
@@ -1077,13 +983,7 @@ impl Error {
1077983
put_usage(&mut c, usage);
1078984
try_help(app, &mut c);
1079985

1080-
Error {
1081-
message: c,
1082-
kind: ErrorKind::UnknownArgument,
1083-
info: vec![arg],
1084-
source: None,
1085-
backtrace: Backtrace::new(),
1086-
}
986+
Self::new(c, ErrorKind::UnknownArgument).set_info(vec![arg])
1087987
}
1088988

1089989
pub(crate) fn argument_not_found_auto(arg: String) -> Self {
@@ -1093,13 +993,7 @@ impl Error {
1093993
c.warning(arg.clone());
1094994
c.none("' wasn't found");
1095995

1096-
Error {
1097-
message: c,
1098-
kind: ErrorKind::ArgumentNotFound,
1099-
info: vec![arg],
1100-
source: None,
1101-
backtrace: Backtrace::new(),
1102-
}
996+
Self::new(c, ErrorKind::ArgumentNotFound).set_info(vec![arg])
1103997
}
1104998

1105999
/// Deprecated, see [`App::error`]

0 commit comments

Comments
 (0)