欢迎页面支持多语言
This commit is contained in:
83
src-tauri/src/app_error.rs
Normal file
83
src-tauri/src/app_error.rs
Normal file
@@ -0,0 +1,83 @@
|
||||
use serde::Serialize;
|
||||
|
||||
use crate::db::error::DbError;
|
||||
|
||||
#[allow(dead_code)]
|
||||
#[derive(Debug, Clone, Copy, Serialize)]
|
||||
#[serde(rename_all = "snake_case")]
|
||||
pub enum AppErrorCode {
|
||||
Unknown,
|
||||
InvalidInput,
|
||||
NotFound,
|
||||
AlreadyExists,
|
||||
PermissionDenied,
|
||||
DependencyMissing,
|
||||
NetworkError,
|
||||
AuthenticationFailed,
|
||||
DatabaseError,
|
||||
IoError,
|
||||
ExternalCommandFailed,
|
||||
WindowOperationFailed,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, thiserror::Error)]
|
||||
#[error("{message}")]
|
||||
pub struct AppCommandError {
|
||||
pub code: AppErrorCode,
|
||||
pub message: String,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub detail: Option<String>,
|
||||
}
|
||||
|
||||
impl AppCommandError {
|
||||
pub fn new(code: AppErrorCode, message: impl Into<String>) -> Self {
|
||||
Self {
|
||||
code,
|
||||
message: message.into(),
|
||||
detail: None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn with_detail(mut self, detail: impl Into<String>) -> Self {
|
||||
self.detail = Some(detail.into());
|
||||
self
|
||||
}
|
||||
|
||||
pub fn db(err: DbError) -> Self {
|
||||
Self::new(AppErrorCode::DatabaseError, "Database operation failed")
|
||||
.with_detail(err.to_string())
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub fn io(err: std::io::Error) -> Self {
|
||||
let code = match err.kind() {
|
||||
std::io::ErrorKind::NotFound => AppErrorCode::NotFound,
|
||||
std::io::ErrorKind::PermissionDenied => AppErrorCode::PermissionDenied,
|
||||
std::io::ErrorKind::AlreadyExists => AppErrorCode::AlreadyExists,
|
||||
_ => AppErrorCode::IoError,
|
||||
};
|
||||
|
||||
let message = match code {
|
||||
AppErrorCode::NotFound => "Resource not found",
|
||||
AppErrorCode::PermissionDenied => "Permission denied",
|
||||
AppErrorCode::AlreadyExists => "Resource already exists",
|
||||
_ => "I/O operation failed",
|
||||
};
|
||||
|
||||
Self::new(code, message).with_detail(err.to_string())
|
||||
}
|
||||
|
||||
pub fn window(message: impl Into<String>, detail: impl Into<String>) -> Self {
|
||||
Self::new(AppErrorCode::WindowOperationFailed, message).with_detail(detail)
|
||||
}
|
||||
|
||||
pub fn external_command(message: impl Into<String>, detail: impl Into<String>) -> Self {
|
||||
Self::new(AppErrorCode::ExternalCommandFailed, message).with_detail(detail)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<DbError> for AppCommandError {
|
||||
fn from(value: DbError) -> Self {
|
||||
Self::db(value)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user