|
| 1 | +use crate::export::ExportError; |
| 2 | + |
| 3 | +use std::{sync::PoisonError, time::Duration}; |
| 4 | +use thiserror::Error; |
| 5 | + |
| 6 | +/// Describe the result of operations in log SDK. |
| 7 | +pub type LogResult<T> = Result<T, LogError>; |
| 8 | + |
| 9 | +#[derive(Error, Debug)] |
| 10 | +#[non_exhaustive] |
| 11 | +/// Errors returned by the log SDK. |
| 12 | +pub enum LogError { |
| 13 | + /// Export failed with the error returned by the exporter. |
| 14 | + #[error("Exporter {} encountered the following errors: {0}", .0.exporter_name())] |
| 15 | + ExportFailed(Box<dyn ExportError>), |
| 16 | + |
| 17 | + /// Export failed to finish after certain period and processor stopped the export. |
| 18 | + #[error("Exporter timed out after {} seconds", .0.as_secs())] |
| 19 | + ExportTimedOut(Duration), |
| 20 | + |
| 21 | + /// Processor is already shutdown |
| 22 | + #[error("{0} already shutdown")] |
| 23 | + AlreadyShutdown(String), |
| 24 | + |
| 25 | + /// Mutex lock poisoning |
| 26 | + #[error("mutex lock poisioning for {0}")] |
| 27 | + MutexPoisoned(String), |
| 28 | + |
| 29 | + /// Other errors propagated from log SDK that weren't covered above. |
| 30 | + #[error(transparent)] |
| 31 | + Other(#[from] Box<dyn std::error::Error + Send + Sync + 'static>), |
| 32 | +} |
| 33 | + |
| 34 | +impl<T> From<T> for LogError |
| 35 | +where |
| 36 | + T: ExportError, |
| 37 | +{ |
| 38 | + fn from(err: T) -> Self { |
| 39 | + LogError::ExportFailed(Box::new(err)) |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +impl From<String> for LogError { |
| 44 | + fn from(err_msg: String) -> Self { |
| 45 | + LogError::Other(Box::new(Custom(err_msg))) |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +impl From<&'static str> for LogError { |
| 50 | + fn from(err_msg: &'static str) -> Self { |
| 51 | + LogError::Other(Box::new(Custom(err_msg.into()))) |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +impl<T> From<PoisonError<T>> for LogError { |
| 56 | + fn from(err: PoisonError<T>) -> Self { |
| 57 | + LogError::Other(err.to_string().into()) |
| 58 | + } |
| 59 | +} |
| 60 | +/// Wrap type for string |
| 61 | +#[derive(Error, Debug)] |
| 62 | +#[error("{0}")] |
| 63 | +struct Custom(String); |
0 commit comments