1use crate::error::ForgeError;
2use std::fmt;
3
4#[derive(Debug)]
13#[non_exhaustive]
14pub struct ContextError<E, C> {
15 pub error: E,
17 pub context: C,
19}
20
21impl<E, C> ContextError<E, C> {
22 pub fn new(error: E, context: C) -> Self {
24 Self { error, context }
25 }
26
27 pub fn into_error(self) -> E {
29 self.error
30 }
31
32 pub fn map_context<D, F>(self, f: F) -> ContextError<E, D>
34 where
35 F: FnOnce(C) -> D,
36 {
37 ContextError {
38 error: self.error,
39 context: f(self.context),
40 }
41 }
42
43 pub fn context<D>(self, context: D) -> ContextError<Self, D>
45 where
46 D: std::fmt::Display + std::fmt::Debug + Send + Sync + 'static,
47 {
48 ContextError::new(self, context)
49 }
50}
51
52impl<E: fmt::Display, C: fmt::Display> fmt::Display for ContextError<E, C> {
53 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
54 write!(f, "{}: {}", self.context, self.error)
55 }
56}
57
58impl<E: std::error::Error + 'static, C: fmt::Display + fmt::Debug + Send + Sync + 'static>
59 std::error::Error for ContextError<E, C>
60{
61 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
62 Some(&self.error)
63 }
64}
65
66pub trait ResultExt<T, E> {
82 fn context<C>(self, context: C) -> Result<T, ContextError<E, C>>;
84
85 fn with_context<C, F>(self, f: F) -> Result<T, ContextError<E, C>>
87 where
88 F: FnOnce() -> C;
89}
90
91impl<T, E> ResultExt<T, E> for Result<T, E> {
92 fn context<C>(self, context: C) -> Result<T, ContextError<E, C>> {
93 self.map_err(|error| ContextError::new(error, context))
94 }
95
96 fn with_context<C, F>(self, f: F) -> Result<T, ContextError<E, C>>
97 where
98 F: FnOnce() -> C,
99 {
100 self.map_err(|error| ContextError::new(error, f()))
101 }
102}
103
104impl<E: ForgeError, C: fmt::Display + fmt::Debug + Send + Sync + 'static> ForgeError
106 for ContextError<E, C>
107{
108 fn kind(&self) -> &'static str {
109 self.error.kind()
110 }
111
112 fn caption(&self) -> &'static str {
113 self.error.caption()
114 }
115
116 fn is_retryable(&self) -> bool {
117 self.error.is_retryable()
118 }
119
120 fn is_fatal(&self) -> bool {
121 self.error.is_fatal()
122 }
123
124 fn status_code(&self) -> u16 {
125 self.error.status_code()
126 }
127
128 fn exit_code(&self) -> i32 {
129 self.error.exit_code()
130 }
131
132 fn user_message(&self) -> String {
136 self.error.user_message()
137 }
138
139 fn dev_message(&self) -> String {
140 format!("{}: {}", self.context, self.error.dev_message())
141 }
142
143 fn backtrace(&self) -> Option<&std::backtrace::Backtrace> {
144 self.error.backtrace()
145 }
146}
147
148#[cfg(test)]
149mod tests {
150 use super::*;
151 use crate::AppError;
152
153 #[test]
154 fn test_context_error() {
155 let error = AppError::config("Invalid config");
156 let ctx_error = error.context("Failed to load settings");
157
158 assert_eq!(
159 ctx_error.to_string(),
160 "Failed to load settings: ⚙️ Configuration Error: Invalid config"
161 );
162 assert_eq!(ctx_error.kind(), "Config");
163 assert_eq!(ctx_error.caption(), "⚙️ Configuration");
164 }
165
166 #[test]
167 fn test_result_context() {
168 let result: Result<(), AppError> = Err(AppError::config("Invalid config"));
169 let ctx_result = result.context("Failed to load settings");
170
171 assert!(ctx_result.is_err());
172 let err = ctx_result.unwrap_err();
173 assert_eq!(
174 err.to_string(),
175 "Failed to load settings: ⚙️ Configuration Error: Invalid config"
176 );
177 }
178}