Skip to main content

joule_profiler_core/source/
error.rs

1use std::{fmt::Debug, time::Duration};
2use thiserror::Error;
3
4/// Errors that can occur when reading or aggregating metrics from a source.
5///
6/// This enum is used by all metric sources implementing [`MetricReader`](`super::MetricReader`)
7/// to signal failures during measurements.
8#[derive(Debug, Error)]
9pub enum MetricSourceError {
10    /// The source failed to retrieve its internal counters.
11    #[error("Error retrieving source counters")]
12    ErrorRetrievingCounters,
13
14    /// The initialization of the source lasted more than the authorized time.
15    #[error(
16        "source initialization timed out after {0:?}. Use --init-timeout to increase the limit."
17    )]
18    InitTimeout(Duration),
19
20    /// Error propagated from a custom metric source.
21    #[error(transparent)]
22    SourceError(#[from] Box<dyn std::error::Error + Send + Sync>),
23}
24
25/// Converts any compatible error into a [`MetricSourceError`].
26///
27/// Implemented for all types that are [`std::error::Error`] + [`Send`] + [`Sync`] + `'static`,
28/// wrapping them in [`MetricSourceError::SourceError`].
29pub trait IntoMetricSourceError {
30    fn into_metric_source_error(self) -> MetricSourceError;
31}
32
33impl<T> IntoMetricSourceError for T
34where
35    T: std::error::Error + Send + Sync + 'static,
36{
37    fn into_metric_source_error(self) -> MetricSourceError {
38        MetricSourceError::SourceError(Box::new(self))
39    }
40}