• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 use std::fmt;
2 use std::path::PathBuf;
3 
4 /// The error type for `tracing-flame`
5 #[derive(Debug)]
6 pub struct Error(pub(crate) Kind);
7 
8 impl Error {
report(&self)9     pub(crate) fn report(&self) {
10         let current_error: &dyn std::error::Error = self;
11         let mut current_error = Some(current_error);
12         let mut ind = 0;
13 
14         eprintln!("Error:");
15 
16         while let Some(error) = current_error {
17             eprintln!("    {}: {}", ind, error);
18             ind += 1;
19             current_error = error.source();
20         }
21     }
22 }
23 
24 impl fmt::Display for Error {
fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result25     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
26         fmt::Display::fmt(&self.0, f)
27     }
28 }
29 
30 impl std::error::Error for Error {
source(&self) -> Option<&(dyn std::error::Error + 'static)>31     fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
32         match &self.0 {
33             Kind::CreateFile { ref source, .. } => Some(source),
34             Kind::FlushFile(ref source) => Some(source),
35         }
36     }
37 }
38 
39 #[derive(Debug)]
40 pub(crate) enum Kind {
41     CreateFile {
42         source: std::io::Error,
43         path: PathBuf,
44     },
45     FlushFile(std::io::Error),
46 }
47 
48 impl fmt::Display for Kind {
fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result49     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
50         match self {
51             Self::CreateFile { path, .. } => {
52                 write!(f, "cannot create output file. path={}", path.display())
53             }
54             Self::FlushFile { .. } => write!(f, "cannot flush output buffer"),
55         }
56     }
57 }
58