TLA Line data Source code
1 : use std::{error::Error as StdError, fmt, io};
2 :
3 : /// Upcast (almost) any error into an opaque [`io::Error`].
4 UBC 0 : pub fn io_error(e: impl Into<Box<dyn StdError + Send + Sync>>) -> io::Error {
5 0 : io::Error::new(io::ErrorKind::Other, e)
6 0 : }
7 :
8 : /// A small combinator for pluggable error logging.
9 0 : pub fn log_error<E: fmt::Display>(e: E) -> E {
10 0 : tracing::error!("{e}");
11 0 : e
12 0 : }
13 :
14 : /// Marks errors that may be safely shown to a client.
15 : /// This trait can be seen as a specialized version of [`ToString`].
16 : ///
17 : /// NOTE: This trait should not be implemented for [`anyhow::Error`], since it
18 : /// is way too convenient and tends to proliferate all across the codebase,
19 : /// ultimately leading to accidental leaks of sensitive data.
20 : pub trait UserFacingError: fmt::Display {
21 : /// Format the error for client, stripping all sensitive info.
22 : ///
23 : /// Although this might be a no-op for many types, it's highly
24 : /// recommended to override the default impl in case error type
25 : /// contains anything sensitive: various IDs, IP addresses etc.
26 : #[inline(always)]
27 0 : fn to_string_client(&self) -> String {
28 0 : self.to_string()
29 0 : }
30 : }
|