Skip to main content

tauri_plugin_fs/
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/// Errors that can happen while using the file system plugin.
10#[derive(Debug, thiserror::Error)]
11#[non_exhaustive]
12pub enum Error {
13    /// JSON serialization or deserialization error.
14    #[error(transparent)]
15    Json(#[from] serde_json::Error),
16    /// Error from the Tauri APIs, usually raised while resolving a path or a scope entry.
17    #[error(transparent)]
18    Tauri(#[from] tauri::Error),
19    /// Error from the underlying file system operation.
20    #[error(transparent)]
21    Io(#[from] std::io::Error),
22    /// The path is denied by the plugin scope or is not allowed by it.
23    #[error("forbidden path: {0}")]
24    PathForbidden(PathBuf),
25    /// Invalid glob pattern.
26    #[error("invalid glob pattern: {0}")]
27    GlobPattern(#[from] glob::PatternError),
28    /// Watcher error.
29    #[cfg(feature = "watch")]
30    #[error(transparent)]
31    Watch(#[from] notify::Error),
32    /// Error invoking the Android plugin implementation.
33    #[cfg(target_os = "android")]
34    #[error(transparent)]
35    PluginInvoke(#[from] tauri::plugin::mobile::PluginInvokeError),
36    /// The URL cannot be converted to a file system path.
37    #[error("URL is not a valid path")]
38    InvalidPathUrl,
39    /// The path is not safe to use, for instance because it traverses parent directories.
40    #[error("Unsafe PathBuf: {0}")]
41    UnsafePathBuf(&'static str),
42}
43
44impl Serialize for Error {
45    fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
46    where
47        S: Serializer,
48    {
49        serializer.serialize_str(self.to_string().as_ref())
50    }
51}