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 General Error: {0}")]
55 PgGeneralError(String),
56
57 #[cfg(feature = "postgres")]
58 #[error("Postgres Error: {0}")]
59 PgError(#[from] sqlx::Error),
60
61 #[cfg(feature = "postgres")]
62 #[error("Postgres DB Error: {0}")]
63 PgDbError(String),
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 #[cfg(feature = "postgres")]
91 DbError::PgError(_) => true,
92 #[cfg(feature = "postgres")]
93 DbError::PgDbError(_) => true,
94 _ => false,
95 }
96 }
97
98 fn metric_label(&self) -> Option<&'static str> {
99 match &self {
100 #[cfg(feature = "bigtable")]
101 DbError::BTError(e) => e.metric_label(),
102 DbError::Backoff(_) => Some("storage.error.backoff"),
103 _ => None,
104 }
105 }
106
107 fn extras(&self) -> Vec<(&str, String)> {
108 match &self {
109 #[cfg(feature = "bigtable")]
110 DbError::BTError(e) => e.extras(),
111 DbError::Backoff(e) => {
112 vec![("raw", e.to_string())]
113 }
114 DbError::Integrity(_, Some(row)) => vec![("row", row.clone())],
115 #[cfg(feature = "postgres")]
116 DbError::PgError(e) => vec![(
117 "error",
118 match e {
119 sqlx::Error::Database(ref db_err) => db_err.message().to_owned(),
120 _ => "No error message".to_owned(),
121 },
122 )],
123 #[cfg(feature = "postgres")]
124 DbError::PgDbError(v) => vec![("error", v.to_owned())],
125 _ => vec![],
126 }
127 }
128}