Alioth Code Coverage

errors.rs66.67%

1// Copyright 2024 Google LLC
2//
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 at
6//
7// https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// 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 and
13// limitations under the License.
14
15use std::error::Error;
16use std::fmt;
17
18use snafu::{ErrorCompat, IntoError, ResultExt};
19
20pub use alioth_macros::{DebugTrace, trace_error};
21
22pub trait DebugTrace: Error {
23 fn debug_trace(&self, f: &mut fmt::Formatter) -> Result<u32, fmt::Error>;
24}
25
26impl 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
32pub trait BoxTrace<'a, T> {
33 fn box_trace<C, E>(self, context: C) -> Result<T, E>
34 where
35 C: IntoError<E, Source = Box<dyn DebugTrace + Send + Sync + 'a>>,
36 E: Error + ErrorCompat;
37}
38
39impl<'a, T, E1> BoxTrace<'a, T> for Result<T, E1>
40where
41 E1: DebugTrace + Send + Sync + 'a,
42{
43 fn box_trace<C, E>(self, context: C) -> Result<T, E>30x
44 where30x
45 C: IntoError<E, Source = Box<dyn DebugTrace + Send + Sync + 'a>>,30x
46 E: Error + ErrorCompat,30x
47 {
48 self.map_err(|e| Box::new(e) as _).context(context)30x
49 }30x
50}
51