trace_tools/
error.rs

1// Copyright 2022 IOTA Stiftung
2// SPDX-License-Identifier: Apache-2.0
3
4use std::{error, fmt, io, path::PathBuf};
5
6/// Error `enum` containing variants for any errors that could potentially occur in this crate.
7#[derive(Debug)]
8pub enum Error {
9    /// Error originating from the [`FlamegraphLayer`](crate::subscriber::layer::FlamegraphLayer).
10    FlamegraphLayer(io::Error),
11    /// Error originating from the [`Flamegrapher`](crate::util::Flamegrapher).
12    Flamegrapher(FlamegrapherErrorKind),
13    /// Error originating from the [`LogLayer`](crate::subscriber::layer::LogLayer).
14    LogLayer(LogLayerErrorKind),
15}
16
17/// An error originating from the [`LogLayer`](crate::subscriber::layer::LogLayer).
18#[derive(Debug)]
19pub enum LogLayerErrorKind {
20    /// Encountered an [`io::Error`].
21    Io(io::Error),
22    /// Error setting the default logger/subscriber.
23    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/// An error originating from the [`FlamegraphLayer`](crate::subscriber::layer::FlamegraphLayer).
48#[derive(Debug)]
49pub enum FlamegraphLayerErrorKind {
50    /// Encountered an [`io::Error`].
51    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/// An error originating from the [`Flamegrapher`](crate::util::Flamegrapher).
69#[derive(Debug)]
70pub enum FlamegrapherErrorKind {
71    /// The provided graph file is invalid.
72    GraphFileInvalid(PathBuf),
73    /// Usage of [`inferno`] resulted in an error.
74    Inferno(Box<dyn error::Error>),
75    /// Encountered an [`io::Error`].
76    Io(io::Error),
77    /// The [`Flamegrapher`](crate::util::Flamegrapher) is missing a required field.
78    MissingField(String),
79    /// Could not find the provided folded stack file.
80    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}