tauri_plugin_advanced_file_manager/opener/
error.rs

1// Copyright 2019-2023 Tauri Programme within The Commons Conservancy
2// SPDX-License-Identifier: Apache-2.0
3// SPDX-License-Identifier: MIT
4
5use std::path::PathBuf;
6
7use serde::{Serialize, Serializer};
8
9#[derive(Debug, thiserror::Error)]
10#[non_exhaustive]
11pub enum Error {
12    #[cfg(mobile)]
13    #[error(transparent)]
14    PluginInvoke(#[from] tauri::plugin::mobile::PluginInvokeError),
15    #[error(transparent)]
16    Tauri(#[from] tauri::Error),
17    #[error(transparent)]
18    Io(#[from] std::io::Error),
19    #[error(transparent)]
20    Json(#[from] serde_json::Error),
21    #[error("unknown program {0}")]
22    UnknownProgramName(String),
23    #[error("Not allowed to open path {}{}", .path, .with.as_ref().map(|w| format!(" with {w}")).unwrap_or_default())]
24    ForbiddenPath { path: String, with: Option<String> },
25    #[error("Not allowed to open url {}{}", .url, .with.as_ref().map(|w| format!(" with {w}")).unwrap_or_default())]
26    ForbiddenUrl { url: String, with: Option<String> },
27    #[error("API not supported on the current platform")]
28    UnsupportedPlatform,
29    #[error(transparent)]
30    #[cfg(windows)]
31    Win32Error(#[from] windows::core::Error),
32    #[error("Path doesn't have a parent: {0}")]
33    NoParent(PathBuf),
34    #[cfg(windows)]
35    #[error("Failed to convert path '{0}' to ITEMIDLIST")]
36    FailedToConvertPathToItemIdList(PathBuf),
37    #[error("Failed to convert path to file:// url")]
38    FailedToConvertPathToFileUrl,
39    #[error(transparent)]
40    #[cfg(any(
41        target_os = "linux",
42        target_os = "dragonfly",
43        target_os = "freebsd",
44        target_os = "netbsd",
45        target_os = "openbsd"
46    ))]
47    Zbus(#[from] zbus::Error),
48}
49
50impl Serialize for Error {
51    fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
52    where
53        S: Serializer,
54    {
55        serializer.serialize_str(self.to_string().as_ref())
56    }
57}
58
59// 添加与主模块 Error 的转换
60impl From<crate::Error> for Error {
61    fn from(err: crate::Error) -> Self {
62        match err {
63            crate::Error::Io(e) => Error::Io(e),
64            crate::Error::Json(e) => Error::Json(e),
65            crate::Error::Tauri(e) => Error::Tauri(e),
66            _ => Error::Io(std::io::Error::new(std::io::ErrorKind::Other, err.to_string())),
67        }
68    }
69}