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 2 : pub fn io_error(e: impl Into<Box<dyn StdError + Send + Sync>>) -> io::Error {
5 2 : io::Error::new(io::ErrorKind::Other, e)
6 2 : }
7 :
8 : /// A small combinator for pluggable error logging.
9 4 : pub fn log_error<E: fmt::Display>(e: E) -> E {
10 4 : tracing::error!("{e}");
11 4 : e
12 4 : }
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 : }
31 :
32 0 : #[derive(Clone)]
33 : pub enum ErrorKind {
34 : /// Wrong password, unknown endpoint, protocol violation, etc...
35 : User,
36 :
37 : /// Network error between user and proxy. Not necessarily user error
38 : Disconnect,
39 :
40 : /// Proxy self-imposed rate limits
41 : RateLimit,
42 :
43 : /// internal errors
44 : Service,
45 :
46 : /// Error communicating with control plane
47 : ControlPlane,
48 :
49 : /// Error communicating with compute
50 : Compute,
51 : }
52 :
53 : impl ErrorKind {
54 0 : pub fn to_str(&self) -> &'static str {
55 0 : match self {
56 0 : ErrorKind::User => "request failed due to user error",
57 0 : ErrorKind::Disconnect => "client disconnected",
58 0 : ErrorKind::RateLimit => "request cancelled due to rate limit",
59 0 : ErrorKind::Service => "internal service error",
60 0 : ErrorKind::ControlPlane => "non-retryable control plane error",
61 0 : ErrorKind::Compute => "non-retryable compute error (or exhausted retry capacity)",
62 : }
63 0 : }
64 : }
|