tauri_plugin_velesdb/
error.rs1use serde::{Deserialize, Serialize};
4use thiserror::Error;
5
6#[derive(Debug, Error)]
8pub enum Error {
9 #[error("Database error: {0}")]
11 Database(#[from] velesdb_core::Error),
12
13 #[error("Collection '{0}' not found")]
15 CollectionNotFound(String),
16
17 #[error("Invalid configuration: {0}")]
19 InvalidConfig(String),
20
21 #[error("Serialization error: {0}")]
23 Serialization(String),
24
25 #[error("IO error: {0}")]
27 Io(#[from] std::io::Error),
28}
29
30#[derive(Debug, Serialize, Deserialize)]
32pub struct CommandError {
33 pub message: String,
35 pub code: String,
37}
38
39impl From<Error> for CommandError {
40 fn from(err: Error) -> Self {
41 let code = match &err {
42 Error::Database(core_err) => core_err.code(),
43 Error::CollectionNotFound(_) => "VELES-002",
44 Error::InvalidConfig(_) => "INVALID_CONFIG",
45 Error::Serialization(_) => "SERIALIZATION_ERROR",
46 Error::Io(_) => "VELES-011",
47 };
48 Self {
49 message: err.to_string(),
50 code: code.to_string(),
51 }
52 }
53}
54
55impl From<serde_json::Error> for Error {
56 fn from(err: serde_json::Error) -> Self {
57 Self::Serialization(err.to_string())
58 }
59}
60
61pub type Result<T> = std::result::Result<T, Error>;
63
64#[cfg(test)]
65mod tests {
66 use super::*;
67
68 #[test]
69 fn test_error_display_collection_not_found() {
70 let err = Error::CollectionNotFound("test_collection".to_string());
72
73 let message = err.to_string();
75
76 assert_eq!(message, "Collection 'test_collection' not found");
78 }
79
80 #[test]
81 fn test_error_display_invalid_config() {
82 let err = Error::InvalidConfig("missing dimension".to_string());
84
85 let message = err.to_string();
87
88 assert_eq!(message, "Invalid configuration: missing dimension");
90 }
91
92 #[test]
93 fn test_command_error_from_error() {
94 let err = Error::CollectionNotFound("docs".to_string());
96
97 let cmd_err: CommandError = err.into();
99
100 assert_eq!(cmd_err.code, "VELES-002");
102 assert!(cmd_err.message.contains("docs"));
103 }
104
105 #[test]
106 fn test_command_error_codes() {
107 let cases = vec![
109 (Error::CollectionNotFound("x".to_string()), "VELES-002"),
110 (Error::InvalidConfig("x".to_string()), "INVALID_CONFIG"),
111 (Error::Serialization("x".to_string()), "SERIALIZATION_ERROR"),
112 ];
113
114 for (err, expected_code) in cases {
115 let cmd_err: CommandError = err.into();
116 assert_eq!(cmd_err.code, expected_code);
117 }
118 }
119
120 #[test]
121 fn test_command_error_database_uses_core_code() {
122 let core_err = velesdb_core::Error::CollectionExists("test".to_string());
124 let err = Error::Database(core_err);
125
126 let cmd_err: CommandError = err.into();
128
129 assert_eq!(cmd_err.code, "VELES-001");
131 }
132
133 #[test]
134 fn test_command_error_io_uses_veles_011() {
135 let io_err = std::io::Error::new(std::io::ErrorKind::NotFound, "file gone");
137 let err = Error::Io(io_err);
138
139 let cmd_err: CommandError = err.into();
141
142 assert_eq!(cmd_err.code, "VELES-011");
144 }
145}