metis_core/error/
conversions.rs1use crate::error::MetisError;
4
5pub trait ErrorContext<T> {
7 fn with_context<F>(self, f: F) -> Result<T, MetisError>
9 where
10 F: FnOnce() -> String;
11
12 fn with_static_context(self, context: &'static str) -> Result<T, MetisError>;
14}
15
16impl<T, E> ErrorContext<T> for Result<T, E>
17where
18 E: Into<MetisError>,
19{
20 fn with_context<F>(self, f: F) -> Result<T, MetisError>
21 where
22 F: FnOnce() -> String,
23 {
24 self.map_err(|e| {
25 let base_error = e.into();
26 MetisError::ValidationFailed {
27 message: format!("{}: {}", f(), base_error),
28 }
29 })
30 }
31
32 fn with_static_context(self, context: &'static str) -> Result<T, MetisError> {
33 self.map_err(|e| {
34 let base_error = e.into();
35 MetisError::ValidationFailed {
36 message: format!("{}: {}", context, base_error),
37 }
38 })
39 }
40}
41
42pub trait UserFriendlyError {
44 fn to_user_message(&self) -> String;
46
47 fn error_category(&self) -> ErrorCategory;
49}
50
51#[derive(Debug, Clone, PartialEq)]
52pub enum ErrorCategory {
53 Workspace,
54 Document,
55 Database,
56 FileSystem,
57 Validation,
58 Network,
59 Configuration,
60}
61
62impl UserFriendlyError for MetisError {
63 fn to_user_message(&self) -> String {
64 match self {
65 MetisError::DocumentNotFound { id } => {
66 format!("Document '{}' could not be found. It may have been moved or deleted.", id)
67 }
68 MetisError::InvalidDocumentType { document_type } => {
69 format!("'{}' is not a valid document type. Valid types are: vision, strategy, initiative, task, adr.", document_type)
70 }
71 MetisError::InvalidPhaseTransition { from, to, doc_type } => {
72 format!("Cannot transition {} from '{}' to '{}'. Please check the valid phase transitions for this document type.", doc_type, from, to)
73 }
74 MetisError::MissingRequiredField { field } => {
75 format!("Required field '{}' is missing. Please provide this information.", field)
76 }
77 MetisError::TemplateNotFound { template } => {
78 format!("Template '{}' could not be found. Please check your template configuration.", template)
79 }
80 MetisError::ValidationFailed { message } => {
81 format!("Validation failed: {}", message)
82 }
83 MetisError::ExitCriteriaNotMet { missing_count, total_count } => {
84 format!("{} of {} exit criteria are incomplete. Please complete all criteria before proceeding.", missing_count, total_count)
85 }
86 MetisError::Database(e) => {
87 format!("Database error: {}. Please try again or contact support if the issue persists.", e)
88 }
89 MetisError::Connection(e) => {
90 format!("Database connection error: {}. Please check your database configuration.", e)
91 }
92 MetisError::Io(e) => {
93 format!("File system error: {}. Please check file permissions and try again.", e)
94 }
95 MetisError::Json(e) => {
96 format!("JSON parsing error: {}. Please check the document format.", e)
97 }
98 MetisError::Yaml(e) => {
99 format!("YAML parsing error: {}. Please check the document format.", e)
100 }
101 _ => {
102 format!("An error occurred: {}. Please try again.", self)
103 }
104 }
105 }
106
107 fn error_category(&self) -> ErrorCategory {
108 match self {
109 MetisError::DocumentNotFound { .. }
110 | MetisError::InvalidDocumentType { .. }
111 | MetisError::TemplateNotFound { .. } => ErrorCategory::Document,
112
113 MetisError::Database(_)
114 | MetisError::Connection(_) => ErrorCategory::Database,
115
116 MetisError::Io(_) => ErrorCategory::FileSystem,
117
118 MetisError::InvalidPhaseTransition { .. }
119 | MetisError::MissingRequiredField { .. }
120 | MetisError::ValidationFailed { .. }
121 | MetisError::ExitCriteriaNotMet { .. } => ErrorCategory::Validation,
122
123 MetisError::Json(_)
124 | MetisError::Yaml(_) => ErrorCategory::Document,
125
126 _ => ErrorCategory::Configuration,
127 }
128 }
129}
130
131#[cfg(test)]
132mod tests {
133 use super::*;
134
135 #[test]
136 fn test_error_context() {
137 let result: Result<(), std::io::Error> = Err(std::io::Error::new(
138 std::io::ErrorKind::NotFound,
139 "file not found"
140 ));
141
142 let with_context = result.with_static_context("Reading configuration file");
143 assert!(with_context.is_err());
144 assert!(with_context.unwrap_err().to_string().contains("Reading configuration file"));
145 }
146
147 #[test]
148 fn test_user_friendly_error_document_not_found() {
149 let error = MetisError::DocumentNotFound { id: "test-doc".to_string() };
150 let message = error.to_user_message();
151 assert!(message.contains("Document 'test-doc' could not be found"));
152 assert_eq!(error.error_category(), ErrorCategory::Document);
153 }
154
155 #[test]
156 fn test_user_friendly_error_validation() {
157 let error = MetisError::ValidationFailed { message: "test validation".to_string() };
158 let message = error.to_user_message();
159 assert!(message.contains("Validation failed: test validation"));
160 assert_eq!(error.error_category(), ErrorCategory::Validation);
161 }
162}