1use std::fmt;
2use std::io;
3use crate::flow::FlowError;
4
5#[derive(Debug)]
7pub enum EditorError {
8 StateError(String),
10 StorageError(String),
12 EventError(String),
14 PluginError(String),
16 IoError(io::Error),
18 ConfigError(String),
20 TransactionError(String),
22 HistoryError(String),
24 EngineError(String),
26 CacheError(String),
28 Unknown(String),
30 Other(String),
32 Flow(FlowError),
34}
35
36impl fmt::Display for EditorError {
37 fn fmt(
38 &self,
39 f: &mut fmt::Formatter<'_>,
40 ) -> fmt::Result {
41 match self {
42 EditorError::StateError(msg) => write!(f, "State error: {}", msg),
43 EditorError::StorageError(msg) => {
44 write!(f, "Storage error: {}", msg)
45 },
46 EditorError::EventError(msg) => write!(f, "Event error: {}", msg),
47 EditorError::PluginError(msg) => write!(f, "Plugin error: {}", msg),
48 EditorError::IoError(err) => write!(f, "IO error: {}", err),
49 EditorError::ConfigError(msg) => write!(f, "Config error: {}", msg),
50 EditorError::TransactionError(msg) => {
51 write!(f, "Transaction error: {}", msg)
52 },
53 EditorError::HistoryError(msg) => {
54 write!(f, "History error: {}", msg)
55 },
56 EditorError::EngineError(msg) => write!(f, "Engine error: {}", msg),
57 EditorError::CacheError(msg) => write!(f, "Cache error: {}", msg),
58 EditorError::Unknown(msg) => write!(f, "Unknown error: {}", msg),
59 EditorError::Other(msg) => write!(f, "Other error: {}", msg),
60 EditorError::Flow(err) => write!(f, "Flow error: {}", err),
61 }
62 }
63}
64
65impl std::error::Error for EditorError {}
66
67impl From<io::Error> for EditorError {
68 fn from(err: io::Error) -> Self {
69 EditorError::IoError(err)
70 }
71}
72
73impl From<String> for EditorError {
74 fn from(err: String) -> Self {
75 EditorError::Unknown(err)
76 }
77}
78
79impl From<&str> for EditorError {
80 fn from(err: &str) -> Self {
81 EditorError::Unknown(err.to_string())
82 }
83}
84
85impl From<FlowError> for EditorError {
86 fn from(err: FlowError) -> Self {
87 EditorError::Flow(err)
88 }
89}
90
91pub type EditorResult<T> = Result<T, EditorError>;
93
94pub mod error_utils {
96 use super::*;
97
98 pub fn map_error<T, E: std::error::Error>(
100 result: Result<T, E>,
101 context: &str,
102 ) -> EditorResult<T> {
103 result.map_err(|e| EditorError::Unknown(format!("{}: {}", context, e)))
104 }
105
106 pub fn state_error(msg: impl Into<String>) -> EditorError {
108 EditorError::StateError(msg.into())
109 }
110
111 pub fn storage_error(msg: impl Into<String>) -> EditorError {
113 EditorError::StorageError(msg.into())
114 }
115
116 pub fn event_error(msg: impl Into<String>) -> EditorError {
118 EditorError::EventError(msg.into())
119 }
120
121 pub fn plugin_error(msg: impl Into<String>) -> EditorError {
123 EditorError::PluginError(msg.into())
124 }
125
126 pub fn config_error(msg: impl Into<String>) -> EditorError {
128 EditorError::ConfigError(msg.into())
129 }
130
131 pub fn transaction_error(msg: impl Into<String>) -> EditorError {
133 EditorError::TransactionError(msg.into())
134 }
135
136 pub fn history_error(msg: impl Into<String>) -> EditorError {
138 EditorError::HistoryError(msg.into())
139 }
140
141 pub fn engine_error(msg: impl Into<String>) -> EditorError {
143 EditorError::EngineError(msg.into())
144 }
145
146 pub fn cache_error(msg: impl Into<String>) -> EditorError {
148 EditorError::CacheError(msg.into())
149 }
150
151 pub fn middleware_error(msg: impl Into<String>) -> EditorError {
153 EditorError::PluginError(msg.into())
154 }
155}