1use std::{error, fmt, io, path::PathBuf};
5
6#[derive(Debug)]
8pub enum Error {
9 FlamegraphLayer(io::Error),
11 Flamegrapher(FlamegrapherErrorKind),
13 LogLayer(LogLayerErrorKind),
15}
16
17#[derive(Debug)]
19pub enum LogLayerErrorKind {
20 Io(io::Error),
22 SetLogger(log::SetLoggerError),
24}
25
26impl fmt::Display for LogLayerErrorKind {
27 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
28 match &self {
29 Self::Io(err) => write!(f, "{}", err),
30 Self::SetLogger(err) => write!(f, "{}", err),
31 }
32 }
33}
34
35impl From<io::Error> for LogLayerErrorKind {
36 fn from(err: io::Error) -> Self {
37 Self::Io(err)
38 }
39}
40
41impl From<log::SetLoggerError> for LogLayerErrorKind {
42 fn from(err: log::SetLoggerError) -> Self {
43 Self::SetLogger(err)
44 }
45}
46
47#[derive(Debug)]
49pub enum FlamegraphLayerErrorKind {
50 Io(io::Error),
52}
53
54impl fmt::Display for FlamegraphLayerErrorKind {
55 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
56 match &self {
57 Self::Io(err) => write!(f, "{}", err),
58 }
59 }
60}
61
62impl From<io::Error> for FlamegraphLayerErrorKind {
63 fn from(err: io::Error) -> Self {
64 Self::Io(err)
65 }
66}
67
68#[derive(Debug)]
70pub enum FlamegrapherErrorKind {
71 GraphFileInvalid(PathBuf),
73 Inferno(Box<dyn error::Error>),
75 Io(io::Error),
77 MissingField(String),
79 StackFileNotFound(PathBuf),
81}
82
83impl fmt::Display for FlamegrapherErrorKind {
84 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
85 match &self {
86 Self::GraphFileInvalid(path) => write!(
87 f,
88 "invalid flamegraph file path: directory {} does not exist",
89 path.to_string_lossy()
90 ),
91 Self::Inferno(err) => write!(f, "{}", err),
92 Self::Io(err) => write!(f, "{}", err),
93 Self::MissingField(field) => write!(f, "flamegrapher builder missing field: {}", field),
94 Self::StackFileNotFound(path) => write!(f, "folded stack file {} does not exist", path.to_string_lossy()),
95 }
96 }
97}
98
99impl fmt::Display for Error {
100 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
101 match &self {
102 Self::FlamegraphLayer(err) => write!(f, "{}", err),
103 Self::Flamegrapher(err) => write!(f, "{}", err),
104 Self::LogLayer(err) => write!(f, "{}", err),
105 }
106 }
107}
108
109impl From<Box<dyn error::Error>> for FlamegrapherErrorKind {
110 fn from(err: Box<dyn error::Error>) -> Self {
111 Self::Inferno(err)
112 }
113}
114
115impl From<io::Error> for FlamegrapherErrorKind {
116 fn from(err: io::Error) -> Self {
117 Self::Io(err)
118 }
119}
120
121impl error::Error for Error {
122 fn source(&self) -> Option<&(dyn error::Error + 'static)> {
123 match &self {
124 Self::FlamegraphLayer(err) => Some(err),
125 Self::Flamegrapher(FlamegrapherErrorKind::Io(err)) => Some(err),
126 Self::LogLayer(LogLayerErrorKind::Io(err)) => Some(err),
127 Self::LogLayer(LogLayerErrorKind::SetLogger(err)) => Some(err),
128 _ => None,
129 }
130 }
131}