headless_lms_utils/error/
backend_error.rs

1/*!
2Contains a common trait for all the error types for this application.
3*/
4
5use backtrace::Backtrace;
6use tracing_error::SpanTrace;
7
8/// The error types of this program all implement this trait for interoperability.
9pub trait BackendError: std::error::Error + std::marker::Sync {
10    type ErrorType: std::fmt::Debug;
11
12    fn new<M: Into<String>, S: Into<Option<anyhow::Error>>>(
13        error_type: Self::ErrorType,
14        message: M,
15        source_error: S,
16    ) -> Self;
17
18    fn new_with_traces<M: Into<String>, S: Into<Option<anyhow::Error>>>(
19        error_type: Self::ErrorType,
20        message: M,
21        source_error: S,
22        backtrace: Backtrace,
23        span_trace: SpanTrace,
24    ) -> Self;
25
26    fn backtrace(&self) -> Option<&Backtrace>;
27
28    fn error_type(&self) -> &Self::ErrorType;
29
30    fn message(&self) -> &str;
31
32    fn span_trace(&self) -> &SpanTrace;
33
34    fn to_different_error<T>(self, new_error_type: T::ErrorType, new_message: String) -> T
35    where
36        T: BackendError,
37        Self: Sized + 'static + std::marker::Send,
38    {
39        T::new(new_error_type, new_message, Some(self.into()))
40    }
41}