| 1 |
// GStreamer |
| 2 |
// |
| 3 |
// Copyright (C) 2015-2023 Sebastian Dröge <[email protected]> |
| 4 |
// |
| 5 |
// This Source Code Form is subject to the terms of the Mozilla Public License, v2.0. |
| 6 |
// If a copy of the MPL was not distributed with this file, You can obtain one at |
| 7 |
// <https://mozilla.org/MPL/2.0/>. |
| 8 |
// |
| 9 |
// SPDX-License-Identifier: MPL-2.0 |
| 10 |
|
| 11 |
use std::{ |
| 12 |
error::Error as StdError, |
| 13 |
fmt::{Debug, Display}, |
| 14 |
}; |
| 15 |
|
| 16 |
/// Custom error type for error display reasons. |
| 17 |
pub struct Error(Box<ErrorInner>); |
| 18 |
|
| 19 |
impl Error { |
| 20 |
#[doc(hidden)] |
| 21 |
pub fn new(message: String, source: Option<Box<dyn StdError + 'static>>) -> Self { |
| 22 |
Error(Box::new(ErrorInner { message, source })) |
| 23 |
} |
| 24 |
} |
| 25 |
|
| 26 |
struct ErrorInner { |
| 27 |
message: String, |
| 28 |
source: Option<Box<dyn StdError + 'static>>, |
| 29 |
} |
| 30 |
|
| 31 |
impl Debug for Error { |
| 32 |
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 33 |
let mut e = self; |
| 34 |
let mut first = true; |
| 35 |
|
| 36 |
// Print the actual error message of this error and then iterate over the whole |
| 37 |
// error chain and print each cause on the chain indented. |
| 38 |
'next_error: loop { |
| 39 |
if first { |
| 40 |
writeln!(f, "{}", e.0.message)?; |
| 41 |
first = false; |
| 42 |
} else { |
| 43 |
for line in e.0.message.lines() { |
| 44 |
writeln!(f, " {}", line)?; |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
let mut source = match e.0.source { |
| 49 |
Some(ref source) => &**source, |
| 50 |
None => break 'next_error, |
| 51 |
}; |
| 52 |
|
| 53 |
if let Some(source) = source.downcast_ref::<Error>() { |
| 54 |
e = source; |
| 55 |
writeln!(f, "\nCaused by:\n")?; |
| 56 |
continue 'next_error; |
| 57 |
} |
| 58 |
|
| 59 |
loop { |
| 60 |
writeln!(f, "\nCaused by:\n")?; |
| 61 |
let source_str = source.to_string(); |
| 62 |
for line in source_str.lines() { |
| 63 |
writeln!(f, " {}", line)?; |
| 64 |
} |
| 65 |
|
| 66 |
source = match source.source() { |
| 67 |
None => break 'next_error, |
| 68 |
Some(source) => source, |
| 69 |
}; |
| 70 |
|
| 71 |
if let Some(source) = source.downcast_ref::<Error>() { |
| 72 |
e = source; |
| 73 |
writeln!(f, "\nCaused by:\n")?; |
| 74 |
continue 'next_error; |
| 75 |
} |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
Ok(()) |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
impl Display for Error { |
| 84 |
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 85 |
<Self as Debug>::fmt(self, f) |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
impl StdError for Error { |
| 90 |
fn source(&self) -> Option<&(dyn StdError + 'static)> { |
| 91 |
match self.0.source { |
| 92 |
None => None, |
| 93 |
Some(ref source) => Some(&**source), |
| 94 |
} |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
impl<'a> From<&'a str> for Error { |
| 99 |
fn from(message: &'a str) -> Self { |
| 100 |
Error(Box::new(ErrorInner { |
| 101 |
message: String::from(message), |
| 102 |
source: None, |
| 103 |
})) |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
impl From<String> for Error { |
| 108 |
fn from(message: String) -> Self { |
| 109 |
Error(Box::new(ErrorInner { |
| 110 |
message, |
| 111 |
source: None, |
| 112 |
})) |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
#[macro_export] |
| 117 |
/// Create a new `Error` from the given message and possibly source error. |
| 118 |
macro_rules! format_err { |
| 119 |
(source: $source:expr, $msg:literal $(,)?) => { |
| 120 |
$crate::error::Error::new( |
| 121 |
String::from($msg), |
| 122 |
Some($source.into()), |
| 123 |
) |
| 124 |
}; |
| 125 |
(source: $source:expr, $err:expr $(,)?) => { |
| 126 |
$crate::error::Error::new( |
| 127 |
format!($err), |
| 128 |
Some($source.into()), |
| 129 |
) |
| 130 |
}; |
| 131 |
(source: $source:expr, $fmt:expr, $($arg:tt)*) => { |
| 132 |
$crate::error::Error::new( |
| 133 |
format!($fmt, $($arg)*), |
| 134 |
Some($source.into()), |
| 135 |
) |
| 136 |
}; |
| 137 |
|
| 138 |
($msg:literal $(,)?) => { |
| 139 |
$crate::error::Error::new( |
| 140 |
String::from($msg), |
| 141 |
None, |
| 142 |
) |
| 143 |
}; |
| 144 |
($err:expr $(,)?) => { |
| 145 |
$crate::error::Error::new( |
| 146 |
format!($err), |
| 147 |
None, |
| 148 |
) |
| 149 |
}; |
| 150 |
($fmt:expr, $($arg:tt)*) => { |
| 151 |
$crate::error::Error::new( |
| 152 |
format!($fmt, $($arg)*), |
| 153 |
None, |
| 154 |
) |
| 155 |
}; |
| 156 |
} |
| 157 |
|
| 158 |
#[macro_export] |
| 159 |
/// Return new `Error` from the given message and possibly source error. |
| 160 |
macro_rules! bail { |
| 161 |
($($arg:tt)+) => { |
| 162 |
return Err($crate::format_err!($($arg)+)); |
| 163 |
}; |
| 164 |
} |
| 165 |
|
| 166 |
/// Trait for adding a context message to any `Result<T, E>` or `Option<T>` |
| 167 |
/// and turning it into a `Result<T, Error>`. |
| 168 |
pub trait Context<T, E> { |
| 169 |
/// Add a static context. |
| 170 |
/// |
| 171 |
/// This should only be called if `context` requires no allocations or otherwise |
| 172 |
/// exists already. |
| 173 |
fn context<C>(self, context: C) -> Result<T, Error> |
| 174 |
where |
| 175 |
C: Display; |
| 176 |
|
| 177 |
/// Add a lazily created context. |
| 178 |
fn with_context<C, F>(self, func: F) -> Result<T, Error> |
| 179 |
where |
| 180 |
C: Display, |
| 181 |
F: FnOnce() -> C; |
| 182 |
} |
| 183 |
|
| 184 |
impl<T, E> Context<T, E> for Result<T, E> |
| 185 |
where |
| 186 |
E: StdError + 'static, |
| 187 |
{ |
| 188 |
fn context<C>(self, context: C) -> Result<T, Error> |
| 189 |
where |
| 190 |
C: Display, |
| 191 |
{ |
| 192 |
self.map_err(|err| Error::new(context.to_string(), Some(Box::new(err)))) |
| 193 |
} |
| 194 |
|
| 195 |
fn with_context<C, F>(self, func: F) -> Result<T, Error> |
| 196 |
where |
| 197 |
C: Display, |
| 198 |
F: FnOnce() -> C, |
| 199 |
{ |
| 200 |
self.map_err(|err| Error::new(func().to_string(), Some(Box::new(err)))) |
| 201 |
} |
| 202 |
} |
| 203 |
|
| 204 |
impl<T> Context<T, Error> for Option<T> { |
| 205 |
fn context<C>(self, context: C) -> Result<T, Error> |
| 206 |
where |
| 207 |
C: Display, |
| 208 |
{ |
| 209 |
self.ok_or_else(|| Error::new(context.to_string(), None)) |
| 210 |
} |
| 211 |
|
| 212 |
fn with_context<C, F>(self, func: F) -> Result<T, Error> |
| 213 |
where |
| 214 |
C: Display, |
| 215 |
F: FnOnce() -> C, |
| 216 |
{ |
| 217 |
self.ok_or_else(|| Error::new(func().to_string(), None)) |
| 218 |
} |
| 219 |
} |