tauri_plugin_py_runtime/
error.rs1use serde::Serialize;
11use serde_json::Value;
12
13pub mod codes {
15 pub const COMMAND_NOT_FOUND: &str = "command_not_found";
17 pub const INVALID_ARGS: &str = "invalid_args";
19 pub const INTERNAL: &str = "internal";
21 pub const CANCELLED: &str = "cancelled";
23 pub const TIMEOUT: &str = "timeout";
25 pub const RUNTIME_UNAVAILABLE: &str = "runtime_unavailable";
27 pub const RUNTIME_EXITED: &str = "runtime_exited";
29 pub const PROTOCOL_MISMATCH: &str = "protocol_mismatch";
31 pub const FRAME_TOO_LARGE: &str = "frame_too_large";
33}
34
35#[derive(Debug, Clone, PartialEq, Serialize, thiserror::Error)]
37#[error("{code}: {message}")]
38pub struct Error {
39 pub code: String,
41 pub message: String,
43 #[serde(skip_serializing_if = "Option::is_none")]
45 pub data: Option<Value>,
46}
47
48impl Error {
49 pub fn new(code: impl Into<String>, message: impl Into<String>) -> Self {
51 Self { code: code.into(), message: message.into(), data: None }
52 }
53
54 pub fn with_data(mut self, data: Value) -> Self {
56 self.data = Some(data);
57 self
58 }
59}
60
61impl From<std::io::Error> for Error {
62 fn from(e: std::io::Error) -> Self {
63 Self::new(codes::INTERNAL, e.to_string())
64 }
65}
66
67impl From<tauri::Error> for Error {
68 fn from(e: tauri::Error) -> Self {
69 Self::new(codes::INTERNAL, e.to_string())
70 }
71}
72
73pub type Result<T> = std::result::Result<T, Error>;