Line data Source code
1 : use std::error::Error as StdError;
2 : use std::{fmt, io};
3 :
4 : use anyhow::Context;
5 : use measured::FixedCardinalityLabel;
6 : use tokio::task::JoinError;
7 :
8 : /// Upcast (almost) any error into an opaque [`io::Error`].
9 0 : pub(crate) fn io_error(e: impl Into<Box<dyn StdError + Send + Sync>>) -> io::Error {
10 0 : io::Error::new(io::ErrorKind::Other, e)
11 0 : }
12 :
13 : /// Marks errors that may be safely shown to a client.
14 : /// This trait can be seen as a specialized version of [`ToString`].
15 : ///
16 : /// NOTE: This trait should not be implemented for [`anyhow::Error`], since it
17 : /// is way too convenient and tends to proliferate all across the codebase,
18 : /// ultimately leading to accidental leaks of sensitive data.
19 : pub(crate) trait UserFacingError: ReportableError {
20 : /// Format the error for client, stripping all sensitive info.
21 : ///
22 : /// Although this might be a no-op for many types, it's highly
23 : /// recommended to override the default impl in case error type
24 : /// contains anything sensitive: various IDs, IP addresses etc.
25 : #[inline(always)]
26 0 : fn to_string_client(&self) -> String {
27 0 : self.to_string()
28 0 : }
29 : }
30 :
31 : #[derive(Copy, Clone, Debug, Eq, PartialEq, FixedCardinalityLabel)]
32 : #[label(singleton = "type")]
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 : #[label(rename = "clientdisconnect")]
39 : ClientDisconnect,
40 :
41 : /// Proxy self-imposed user rate limits
42 : #[label(rename = "ratelimit")]
43 : RateLimit,
44 :
45 : /// Proxy self-imposed service-wise rate limits
46 : #[label(rename = "serviceratelimit")]
47 : ServiceRateLimit,
48 :
49 : /// Proxy quota limit violation
50 : #[label(rename = "quota")]
51 : Quota,
52 :
53 : /// internal errors
54 : Service,
55 :
56 : /// Error communicating with control plane
57 : #[label(rename = "controlplane")]
58 : ControlPlane,
59 :
60 : /// Postgres error
61 : Postgres,
62 :
63 : /// Error communicating with compute
64 : Compute,
65 : }
66 :
67 : impl ErrorKind {
68 0 : pub(crate) fn to_metric_label(self) -> &'static str {
69 0 : match self {
70 0 : ErrorKind::User => "user",
71 0 : ErrorKind::ClientDisconnect => "clientdisconnect",
72 0 : ErrorKind::RateLimit => "ratelimit",
73 0 : ErrorKind::ServiceRateLimit => "serviceratelimit",
74 0 : ErrorKind::Quota => "quota",
75 0 : ErrorKind::Service => "service",
76 0 : ErrorKind::ControlPlane => "controlplane",
77 0 : ErrorKind::Postgres => "postgres",
78 0 : ErrorKind::Compute => "compute",
79 : }
80 0 : }
81 : }
82 :
83 : pub(crate) trait ReportableError: fmt::Display + Send + 'static {
84 : fn get_error_kind(&self) -> ErrorKind;
85 : }
86 :
87 : impl ReportableError for postgres_client::error::Error {
88 0 : fn get_error_kind(&self) -> ErrorKind {
89 0 : if self.as_db_error().is_some() {
90 0 : ErrorKind::Postgres
91 : } else {
92 0 : ErrorKind::Compute
93 : }
94 0 : }
95 : }
96 :
97 : /// Flattens `Result<Result<T>>` into `Result<T>`.
98 0 : pub fn flatten_err<T>(r: Result<anyhow::Result<T>, JoinError>) -> anyhow::Result<T> {
99 0 : r.context("join error").and_then(|x| x)
100 0 : }
|