errors.rs66.67%
1
// Copyright 2024 Google LLC2
//3
// Licensed under the Apache License, Version 2.0 (the "License");4
// you may not use this file except in compliance with the License.5
// You may obtain a copy of the License at6
//7
// https://www.apache.org/licenses/LICENSE-2.08
//9
// Unless required by applicable law or agreed to in writing, software10
// distributed under the License is distributed on an "AS IS" BASIS,11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.12
// See the License for the specific language governing permissions and13
// limitations under the License.14
15
use std::error::Error;16
use std::fmt;17
18
use snafu::{ErrorCompat, IntoError, ResultExt};19
20
pub use alioth_macros::{DebugTrace, trace_error};21
22
pub trait DebugTrace: Error {23
fn debug_trace(&self, f: &mut fmt::Formatter) -> Result<u32, fmt::Error>;24
}25
26
impl Error for Box<dyn DebugTrace + Send + Sync + 'static> {27
fn source(&self) -> Option<&(dyn Error + 'static)> {28
Error::source(Box::as_ref(self))29
}30
}31
32
pub trait BoxTrace<'a, T> {33
fn box_trace<C, E>(self, context: C) -> Result<T, E>34
where35
C: IntoError<E, Source = Box<dyn DebugTrace + Send + Sync + 'a>>,36
E: Error + ErrorCompat;37
}38
39
impl<'a, T, E1> BoxTrace<'a, T> for Result<T, E1>40
where41
E1: DebugTrace + Send + Sync + 'a,42
{43
fn box_trace<C, E>(self, context: C) -> Result<T, E>30x44
where30x45
C: IntoError<E, Source = Box<dyn DebugTrace + Send + Sync + 'a>>,30x46
E: Error + ErrorCompat,30x47
{48
self.map_err(|e| Box::new(e) as _).context(context)30x49
}30x50
}51