Move error to different file and add thiserror impl
parent
4e37078758
commit
36c5f28b1c
@ -0,0 +1,75 @@
|
||||
use std::fmt::Debug;
|
||||
|
||||
use crate::internal::exit_code::AppExitCode;
|
||||
use crate::{fl, fl_crash};
|
||||
use thiserror::Error;
|
||||
|
||||
pub type AppResult<T> = Result<T, AppError>;
|
||||
|
||||
#[derive(Debug, Error)]
|
||||
#[allow(clippy::module_name_repetitions)]
|
||||
pub enum AppError {
|
||||
#[error("IoError: {0}")]
|
||||
Io(#[from] std::io::Error),
|
||||
|
||||
#[error("AUR RPC call failed {0}")]
|
||||
Rpc(#[from] aur_rpc::error::RPCError),
|
||||
|
||||
#[error("{}", fl!("build-step-violation"))]
|
||||
BuildStepViolation,
|
||||
|
||||
#[error("{} {pkg_name}", fl!("build-error"))]
|
||||
BuildError { pkg_name: String },
|
||||
|
||||
#[error("{}", fl!("user-cancel"))]
|
||||
UserCancellation,
|
||||
|
||||
#[error("{} {}",fl!("missing-deps"), _0.join(", ") )]
|
||||
MissingDependencies(Vec<String>),
|
||||
|
||||
#[error("{} {0}", fl!("makepkg-err"))]
|
||||
MakePkg(String),
|
||||
|
||||
#[error(transparent)]
|
||||
MinusError(#[from] minus::MinusError),
|
||||
|
||||
#[error(transparent)]
|
||||
FmtError(#[from] std::fmt::Error),
|
||||
|
||||
#[error(transparent)]
|
||||
AlpmError(#[from] crate::internal::alpm::Error),
|
||||
|
||||
#[error("{}", fl!("non-zero-exit"))]
|
||||
NonZeroExit,
|
||||
|
||||
#[error("{0}")]
|
||||
Other(String),
|
||||
}
|
||||
|
||||
impl From<String> for AppError {
|
||||
fn from(string: String) -> Self {
|
||||
Self::Other(string)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&str> for AppError {
|
||||
fn from(string: &str) -> Self {
|
||||
Self::from(string.to_string())
|
||||
}
|
||||
}
|
||||
|
||||
pub trait SilentUnwrap<T> {
|
||||
fn silent_unwrap(self, error_code: AppExitCode) -> T;
|
||||
}
|
||||
|
||||
impl<T> SilentUnwrap<T> for AppResult<T> {
|
||||
fn silent_unwrap(self, exit_code: AppExitCode) -> T {
|
||||
match self {
|
||||
Ok(val) => val,
|
||||
Err(e) => {
|
||||
tracing::debug!("{e}");
|
||||
fl_crash!(exit_code, "error-occurred")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -1,106 +0,0 @@
|
||||
use std::error::Error;
|
||||
use std::fmt::{Debug, Display, Formatter};
|
||||
use std::io;
|
||||
|
||||
use crate::internal::exit_code::AppExitCode;
|
||||
use crate::{fl, fl_crash};
|
||||
|
||||
pub type AppResult<T> = Result<T, AppError>;
|
||||
|
||||
#[derive(Debug)]
|
||||
#[allow(clippy::module_name_repetitions)]
|
||||
pub enum AppError {
|
||||
Io(std::io::Error),
|
||||
Other(String),
|
||||
Rpc(aur_rpc::error::RPCError),
|
||||
NonZeroExit,
|
||||
BuildStepViolation,
|
||||
BuildError { pkg_name: String },
|
||||
UserCancellation,
|
||||
MissingDependencies(Vec<String>),
|
||||
MakePkg(String),
|
||||
MinusError(minus::MinusError),
|
||||
FmtError(std::fmt::Error),
|
||||
AlpmError(crate::internal::alpm::Error),
|
||||
}
|
||||
|
||||
impl Display for AppError {
|
||||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
||||
match self {
|
||||
AppError::Io(io) => Display::fmt(io, f),
|
||||
AppError::Rpc(e) => Display::fmt(e, f),
|
||||
AppError::Other(s) => Display::fmt(s, f),
|
||||
AppError::NonZeroExit => Display::fmt(&fl!("non-zero-exit"), f),
|
||||
AppError::BuildStepViolation => Display::fmt(&fl!("build-step-violation"), f),
|
||||
AppError::BuildError { pkg_name } => write!(f, "{} {pkg_name}", fl!("build-error")),
|
||||
AppError::UserCancellation => write!(f, "{}", fl!("user-cancel")),
|
||||
AppError::MissingDependencies(deps) => {
|
||||
write!(f, "{} {}", fl!("missing-deps"), deps.join(", "))
|
||||
}
|
||||
AppError::MakePkg(msg) => write!(f, "{} {msg}", fl!("makepkg-err")),
|
||||
AppError::MinusError(e) => Display::fmt(e, f),
|
||||
AppError::FmtError(e) => Display::fmt(e, f),
|
||||
AppError::AlpmError(e) => Display::fmt(e, f),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Error for AppError {}
|
||||
|
||||
impl From<io::Error> for AppError {
|
||||
fn from(e: io::Error) -> Self {
|
||||
Self::Io(e)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<aur_rpc::error::RPCError> for AppError {
|
||||
fn from(e: aur_rpc::error::RPCError) -> Self {
|
||||
Self::Rpc(e)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<String> for AppError {
|
||||
fn from(string: String) -> Self {
|
||||
Self::Other(string)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&str> for AppError {
|
||||
fn from(string: &str) -> Self {
|
||||
Self::from(string.to_string())
|
||||
}
|
||||
}
|
||||
|
||||
impl From<minus::MinusError> for AppError {
|
||||
fn from(e: minus::MinusError) -> Self {
|
||||
Self::MinusError(e)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<std::fmt::Error> for AppError {
|
||||
fn from(e: std::fmt::Error) -> Self {
|
||||
Self::FmtError(e)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<crate::internal::alpm::Error> for AppError {
|
||||
fn from(e: crate::internal::alpm::Error) -> Self {
|
||||
Self::AlpmError(e)
|
||||
}
|
||||
}
|
||||
|
||||
pub trait SilentUnwrap<T> {
|
||||
fn silent_unwrap(self, error_code: AppExitCode) -> T;
|
||||
}
|
||||
|
||||
impl<T> SilentUnwrap<T> for AppResult<T> {
|
||||
fn silent_unwrap(self, exit_code: AppExitCode) -> T {
|
||||
match self {
|
||||
Ok(val) => val,
|
||||
Err(e) => {
|
||||
tracing::debug!("{e}");
|
||||
fl_crash!(exit_code, "error-occurred")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue