cli: extract CommandError::with_message() constructor

This commit is contained in:
Yuya Nishihara 2024-03-29 20:07:26 +09:00
parent 1e83faf4f8
commit 91ff1fdd1e

View file

@ -72,6 +72,14 @@ impl CommandError {
} }
} }
pub fn with_message(
kind: CommandErrorKind,
message: impl Into<String>,
source: impl Into<Box<dyn error::Error + Send + Sync>>,
) -> Self {
Self::new(kind, ErrorWithMessage::new(message, source))
}
/// Returns error with the given plain-text `hint` attached. /// Returns error with the given plain-text `hint` attached.
pub fn hinted(mut self, hint: impl Into<String>) -> Self { pub fn hinted(mut self, hint: impl Into<String>) -> Self {
self.add_hint(hint); self.add_hint(hint);
@ -146,7 +154,7 @@ pub fn user_error_with_message(
message: impl Into<String>, message: impl Into<String>,
source: impl Into<Box<dyn error::Error + Send + Sync>>, source: impl Into<Box<dyn error::Error + Send + Sync>>,
) -> CommandError { ) -> CommandError {
user_error(ErrorWithMessage::new(message, source)) CommandError::with_message(CommandErrorKind::User, message, source)
} }
pub fn config_error(err: impl Into<Box<dyn error::Error + Send + Sync>>) -> CommandError { pub fn config_error(err: impl Into<Box<dyn error::Error + Send + Sync>>) -> CommandError {
@ -157,7 +165,7 @@ pub fn config_error_with_message(
message: impl Into<String>, message: impl Into<String>,
source: impl Into<Box<dyn error::Error + Send + Sync>>, source: impl Into<Box<dyn error::Error + Send + Sync>>,
) -> CommandError { ) -> CommandError {
config_error(ErrorWithMessage::new(message, source)) CommandError::with_message(CommandErrorKind::Config, message, source)
} }
pub fn cli_error(err: impl Into<Box<dyn error::Error + Send + Sync>>) -> CommandError { pub fn cli_error(err: impl Into<Box<dyn error::Error + Send + Sync>>) -> CommandError {
@ -172,7 +180,7 @@ pub fn internal_error_with_message(
message: impl Into<String>, message: impl Into<String>,
source: impl Into<Box<dyn error::Error + Send + Sync>>, source: impl Into<Box<dyn error::Error + Send + Sync>>,
) -> CommandError { ) -> CommandError {
internal_error(ErrorWithMessage::new(message, source)) CommandError::with_message(CommandErrorKind::Internal, message, source)
} }
fn format_similarity_hint<S: AsRef<str>>(candidates: &[S]) -> Option<String> { fn format_similarity_hint<S: AsRef<str>>(candidates: &[S]) -> Option<String> {