1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

//! Helpers for using the tracing crate more effectively.
//!
//! In particular, this includes extension traits to make it easier to pass
//! errors to tracing events. The events can take a `dyn Error`, but getting a
//! `dyn Error` from an arbitrary error type requires casting.
//!
//! Hopefully this crate will be short lived as `tracing` ergonomics continue
//! to improve.

pub mod formatter;

/// Extension trait to make it easy to trace anyhow errors.
pub trait AnyhowValueExt {
    /// Returns the error as a type that can be traced.
    fn as_error(&self) -> &(dyn 'static + std::error::Error);
}

impl AnyhowValueExt for anyhow::Error {
    fn as_error(&self) -> &(dyn 'static + std::error::Error) {
        &**self
    }
}

/// Extension trait to make it easy to trace errors.
pub trait ErrorValueExt {
    /// Returns the error as a type that can be traced.
    fn as_error(&self) -> &(dyn 'static + std::error::Error);
}

impl<T> ErrorValueExt for T
where
    T: 'static + std::error::Error,
{
    fn as_error(&self) -> &(dyn 'static + std::error::Error) {
        self
    }
}