autopush_common/db/
error.rs1use actix_web::http::StatusCode;
2use serde_json::Error as Serde_Error;
3
4use thiserror::Error;
5
6#[cfg(feature = "bigtable")]
7use crate::db::bigtable::BigTableError;
8use crate::errors::ReportableError;
9
10pub type DbResult<T> = Result<T, DbError>;
11
12#[derive(Debug, Error)]
13pub enum DbError {
14 #[error("Error while performing (de)serialization: {0}")]
15 Serialization(String),
16
17 #[error("Error deserializing to u64: {0}")]
18 DeserializeU64(String),
19
20 #[error("Error deserializing to String: {0}")]
21 DeserializeString(String),
22
23 #[error("Unable to determine table status")]
24 TableStatusUnknown,
25
26 #[cfg(feature = "bigtable")]
27 #[error("BigTable error: {0}")]
28 BTError(#[from] BigTableError),
29
30 #[cfg(feature = "redis")]
31 #[error("Redis error {0}")]
32 RedisError(#[from] redis::RedisError),
33
34 #[error("Serde Parse Error {0}")]
35 SerdeError(#[from] Serde_Error),
36
37 #[error("Connection failure: {0}")]
38 ConnectionError(String),
39
40 #[error("The conditional request failed")]
41 Conditional,
42
43 #[error("Database integrity error: {}", _0)]
44 Integrity(String, Option<String>),
45
46 #[error("Unknown Database Error: {0}")]
47 General(String),
48
49 #[error("Process pending, please wait.")]
51 Backoff(String),
52
53 #[cfg(feature = "postgres")]
54 #[error("Postgres Error: {0}")]
55 PgGeneralError(String),
56
57 #[cfg(feature = "postgres")]
58 #[error("Postgres Error: {0}")]
59 PgPoolError(#[from] deadpool::managed::PoolError<tokio_postgres::Error>),
60
61 #[cfg(feature = "postgres")]
62 #[error("Postgres Error: {0}")]
63 PgError(#[from] tokio_postgres::Error),
64}
65
66impl DbError {
67 pub fn status(&self) -> StatusCode {
68 match self {
69 #[cfg(feature = "bigtable")]
70 Self::BTError(e) => e.status(),
71 Self::Backoff(_) => StatusCode::SERVICE_UNAVAILABLE,
72 _ => StatusCode::INTERNAL_SERVER_ERROR,
73 }
74 }
75}
76
77impl ReportableError for DbError {
78 fn reportable_source(&self) -> Option<&(dyn ReportableError + 'static)> {
79 match &self {
80 #[cfg(feature = "bigtable")]
81 DbError::BTError(e) => Some(e),
82 _ => None,
83 }
84 }
85
86 fn is_sentry_event(&self) -> bool {
87 match &self {
88 #[cfg(feature = "bigtable")]
89 DbError::BTError(e) => e.is_sentry_event(),
90 _ => false,
91 }
92 }
93
94 fn metric_label(&self) -> Option<&'static str> {
95 match &self {
96 #[cfg(feature = "bigtable")]
97 DbError::BTError(e) => e.metric_label(),
98 DbError::Backoff(_) => Some("storage.error.backoff"),
99 _ => None,
100 }
101 }
102
103 fn extras(&self) -> Vec<(&str, String)> {
104 match &self {
105 #[cfg(feature = "bigtable")]
106 DbError::BTError(e) => e.extras(),
107 DbError::Backoff(e) => {
108 vec![("raw", e.to_string())]
109 }
110 DbError::Integrity(_, Some(row)) => vec![("row", row.clone())],
111 _ => vec![],
112 }
113 }
114}