moduforge_runtime/
error.rs1use std::fmt;
2use std::io;
3
4#[derive(Debug)]
6pub enum EditorError {
7 StateError(String),
9 StorageError(String),
11 EventError(String),
13 PluginError(String),
15 IoError(io::Error),
17 ConfigError(String),
19 TransactionError(String),
21 HistoryError(String),
23 EngineError(String),
25 CacheError(String),
27 Unknown(String),
29}
30
31impl fmt::Display for EditorError {
32 fn fmt(
33 &self,
34 f: &mut fmt::Formatter<'_>,
35 ) -> fmt::Result {
36 match self {
37 EditorError::StateError(msg) => write!(f, "State error: {}", msg),
38 EditorError::StorageError(msg) => {
39 write!(f, "Storage error: {}", msg)
40 },
41 EditorError::EventError(msg) => write!(f, "Event error: {}", msg),
42 EditorError::PluginError(msg) => write!(f, "Plugin error: {}", msg),
43 EditorError::IoError(err) => write!(f, "IO error: {}", err),
44 EditorError::ConfigError(msg) => write!(f, "Config error: {}", msg),
45 EditorError::TransactionError(msg) => {
46 write!(f, "Transaction error: {}", msg)
47 },
48 EditorError::HistoryError(msg) => {
49 write!(f, "History error: {}", msg)
50 },
51 EditorError::EngineError(msg) => write!(f, "Engine error: {}", msg),
52 EditorError::CacheError(msg) => write!(f, "Cache error: {}", msg),
53 EditorError::Unknown(msg) => write!(f, "Unknown error: {}", msg),
54 }
55 }
56}
57
58impl std::error::Error for EditorError {}
59
60impl From<io::Error> for EditorError {
61 fn from(err: io::Error) -> Self {
62 EditorError::IoError(err)
63 }
64}
65
66impl From<String> for EditorError {
67 fn from(err: String) -> Self {
68 EditorError::Unknown(err)
69 }
70}
71
72impl From<&str> for EditorError {
73 fn from(err: &str) -> Self {
74 EditorError::Unknown(err.to_string())
75 }
76}
77
78pub type EditorResult<T> = Result<T, EditorError>;
80
81pub mod error_utils {
83 use super::*;
84
85 pub fn map_error<T, E: std::error::Error>(
87 result: Result<T, E>,
88 context: &str,
89 ) -> EditorResult<T> {
90 result.map_err(|e| EditorError::Unknown(format!("{}: {}", context, e)))
91 }
92
93 pub fn state_error(msg: impl Into<String>) -> EditorError {
95 EditorError::StateError(msg.into())
96 }
97
98 pub fn storage_error(msg: impl Into<String>) -> EditorError {
100 EditorError::StorageError(msg.into())
101 }
102
103 pub fn event_error(msg: impl Into<String>) -> EditorError {
105 EditorError::EventError(msg.into())
106 }
107
108 pub fn plugin_error(msg: impl Into<String>) -> EditorError {
110 EditorError::PluginError(msg.into())
111 }
112
113 pub fn config_error(msg: impl Into<String>) -> EditorError {
115 EditorError::ConfigError(msg.into())
116 }
117
118 pub fn transaction_error(msg: impl Into<String>) -> EditorError {
120 EditorError::TransactionError(msg.into())
121 }
122
123 pub fn history_error(msg: impl Into<String>) -> EditorError {
125 EditorError::HistoryError(msg.into())
126 }
127
128 pub fn engine_error(msg: impl Into<String>) -> EditorError {
130 EditorError::EngineError(msg.into())
131 }
132
133 pub fn cache_error(msg: impl Into<String>) -> EditorError {
135 EditorError::CacheError(msg.into())
136 }
137}