Skip to main content

tauri_plugin_video/
error.rs

1use serde::{ser::Serializer, Serialize};
2
3pub type Result<T> = std::result::Result<T, Error>;
4
5#[derive(Debug, thiserror::Error)]
6pub enum Error {
7    #[error("invalid request: {0}")]
8    InvalidRequest(String),
9    #[error("GStreamer is unavailable: {0}")]
10    RuntimeUnavailable(String),
11    #[error("media pipeline failed: {0}")]
12    Pipeline(String),
13    #[cfg(mobile)]
14    #[error(transparent)]
15    PluginInvoke(#[from] tauri::plugin::mobile::PluginInvokeError),
16}
17
18impl Error {
19    pub fn code(&self) -> &'static str {
20        match self {
21            Self::InvalidRequest(_) => "INVALID_REQUEST",
22            Self::RuntimeUnavailable(_) => "RUNTIME_UNAVAILABLE",
23            Self::Pipeline(_) => "PIPELINE_FAILED",
24            #[cfg(mobile)]
25            Self::PluginInvoke(_) => "MOBILE_PLUGIN_ERROR",
26        }
27    }
28
29    fn recoverable(&self) -> bool {
30        matches!(self, Self::Pipeline(_))
31    }
32}
33
34impl Serialize for Error {
35    fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
36    where
37        S: Serializer,
38    {
39        #[derive(Serialize)]
40        #[serde(rename_all = "camelCase")]
41        struct WireError<'a> {
42            code: &'static str,
43            message: String,
44            recoverable: bool,
45            #[serde(skip_serializing_if = "Option::is_none")]
46            stage: Option<&'a str>,
47        }
48
49        WireError {
50            code: self.code(),
51            message: self.to_string(),
52            recoverable: self.recoverable(),
53            stage: match self {
54                Self::Pipeline(_) => Some("pipeline"),
55                _ => None,
56            },
57        }
58        .serialize(serializer)
59    }
60}