tauri_plugin_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/// The error type returned by the opener plugin's commands and APIs.
10#[derive(Debug, thiserror::Error)]
11#[non_exhaustive]
12pub enum Error {
13 /// Forwarded from a failed call into the mobile plugin runtime.
14 #[cfg(mobile)]
15 #[error(transparent)]
16 PluginInvoke(#[from] tauri::plugin::mobile::PluginInvokeError),
17 /// Forwarded from a [`tauri::Error`], e.g. when resolving a scoped path fails.
18 #[error(transparent)]
19 Tauri(#[from] tauri::Error),
20 /// Forwarded from an [`std::io::Error`] raised while opening or revealing a path.
21 #[error(transparent)]
22 Io(#[from] std::io::Error),
23 /// Forwarded from a [`serde_json::Error`] raised while (de)serializing a mobile plugin payload.
24 #[error(transparent)]
25 Json(#[from] serde_json::Error),
26 /// The program passed as `with` is not a program name known by the underlying opener.
27 #[error("unknown program {0}")]
28 UnknownProgramName(String),
29 /// The path is not allowed by the opener scope, optionally together with the program it was requested to be opened with.
30 #[error("Not allowed to open path {}{}", .path, .with.as_ref().map(|w| format!(" with {w}")).unwrap_or_default())]
31 ForbiddenPath {
32 /// The path that was rejected by the scope.
33 path: String,
34 /// The program the path was requested to be opened with, if any.
35 with: Option<String>,
36 },
37 /// The URL is not allowed by the opener scope, optionally together with the program it was requested to be opened with.
38 #[error("Not allowed to open url {}{}", .url, .with.as_ref().map(|w| format!(" with {w}")).unwrap_or_default())]
39 ForbiddenUrl {
40 /// The URL that was rejected by the scope.
41 url: String,
42 /// The program the URL was requested to be opened with, if any.
43 with: Option<String>,
44 },
45 /// The requested API is not supported on the current platform, e.g. [`crate::reveal_item_in_dir`] on Android and iOS.
46 #[error("API not supported on the current platform")]
47 UnsupportedPlatform,
48 /// Forwarded from a Win32 API call, see [`windows::core::Error`].
49 #[error(transparent)]
50 #[cfg(windows)]
51 Win32Error(#[from] windows::core::Error),
52 /// The given path has no parent directory, so it cannot be revealed in its containing folder.
53 #[error("Path doesn't have a parent: {0}")]
54 NoParent(PathBuf),
55 // TODO: Add the underlying io::Error to this variant
56 /// Failed to convert the path to a Windows `ITEMIDLIST` while preparing it to be revealed in the file explorer.
57 #[cfg(windows)]
58 #[error("Failed to convert path '{0}' to ITEMIDLIST")]
59 FailedToConvertPathToItemIdList(PathBuf),
60 /// Failed to convert the path to a `file://` URL, which is required to reveal it via D-Bus on Linux and BSD.
61 #[error("Failed to convert path to file:// url")]
62 FailedToConvertPathToFileUrl,
63 /// Forwarded from a [`zbus::Error`] raised while talking to the file manager or the desktop portal over D-Bus.
64 #[error(transparent)]
65 #[cfg(any(
66 target_os = "linux",
67 target_os = "dragonfly",
68 target_os = "freebsd",
69 target_os = "netbsd",
70 target_os = "openbsd"
71 ))]
72 Zbus(#[from] zbus::Error),
73}
74
75impl Serialize for Error {
76 fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
77 where
78 S: Serializer,
79 {
80 serializer.serialize_str(self.to_string().as_ref())
81 }
82}