xref: /DragonOS/tools/debugging/logmonitor/src/backend/error.rs (revision c635d8a9cfe25bc11779f323ef0c7d7a0f597d4a)
1 use std::{error::Error, fmt::Display};
2 
3 #[derive(Debug)]
4 pub enum BackendErrorKind {
5     FileNotFound,
6     KernelLoadError,
7 }
8 
9 #[derive(Debug)]
10 pub struct BackendError {
11     kind: BackendErrorKind,
12     message: Option<String>,
13 }
14 
15 impl BackendError {
16     pub fn new(kind: BackendErrorKind, message: Option<String>) -> Self {
17         Self { kind, message }
18     }
19 }
20 
21 impl Error for BackendError {}
22 
23 impl Display for BackendError {
24     fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
25         match &self.kind {
26             BackendErrorKind::FileNotFound => {
27                 write!(f, "File not found: {:?}", self.message.as_ref().unwrap())
28             }
29             BackendErrorKind::KernelLoadError => {
30                 write!(
31                     f,
32                     "Failed to load kernel: {:?}",
33                     self.message.as_ref().unwrap()
34                 )
35             }
36         }
37     }
38 }
39