Skip to main content

tauri_plugin_shell/
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 returned by the shell plugin.
10#[derive(Debug, thiserror::Error)]
11pub enum Error {
12    /// Failed to invoke the mobile plugin implementation.
13    ///
14    /// Only available on mobile.
15    #[cfg(mobile)]
16    #[error(transparent)]
17    PluginInvoke(#[from] tauri::plugin::mobile::PluginInvokeError),
18    /// I/O error, usually raised when spawning a process or reading its output.
19    #[error(transparent)]
20    Io(#[from] std::io::Error),
21    /// The path of the current executable has no parent directory,
22    /// so the path of a sidecar program could not be resolved.
23    #[error("current executable path has no parent")]
24    CurrentExeHasNoParent,
25    /// The string does not match any of the known programs
26    /// accepted by the deprecated `open` API.
27    #[error("unknown program {0}")]
28    UnknownProgramName(String),
29    /// The command is not allowed by the configured shell scope.
30    #[error(transparent)]
31    Scope(#[from] crate::scope::Error),
32    /// Sidecar not allowed by the configuration.
33    #[error("sidecar not configured under `tauri.conf.json > bundle > externalBin`: {0}")]
34    SidecarNotAllowed(PathBuf),
35    /// Program not allowed by the scope.
36    #[error("program not allowed on the configured shell scope: {0}")]
37    ProgramNotAllowed(PathBuf),
38    /// The `encoding` spawn option is neither `raw` nor a label of a known character encoding.
39    #[error("unknown encoding {0}")]
40    UnknownEncoding(String),
41    /// JSON error.
42    #[error(transparent)]
43    Json(#[from] serde_json::Error),
44    /// Utf8 error.
45    #[error(transparent)]
46    Utf8(#[from] std::string::FromUtf8Error),
47}
48
49impl Serialize for Error {
50    fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
51    where
52        S: Serializer,
53    {
54        serializer.serialize_str(self.to_string().as_ref())
55    }
56}