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 Json 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
54impl DbError {
55 pub fn status(&self) -> StatusCode {
56 match self {
57 #[cfg(feature = "bigtable")]
58 Self::BTError(e) => e.status(),
59 Self::Backoff(_) => StatusCode::SERVICE_UNAVAILABLE,
60 _ => StatusCode::INTERNAL_SERVER_ERROR,
61 }
62 }
63}
64
65impl ReportableError for DbError {
66 fn reportable_source(&self) -> Option<&(dyn ReportableError + 'static)> {
67 match &self {
68 #[cfg(feature = "bigtable")]
69 DbError::BTError(e) => Some(e),
70 _ => None,
71 }
72 }
73
74 fn is_sentry_event(&self) -> bool {
75 match &self {
76 #[cfg(feature = "bigtable")]
77 DbError::BTError(e) => e.is_sentry_event(),
78 _ => false,
79 }
80 }
81
82 fn metric_label(&self) -> Option<&'static str> {
83 match &self {
84 #[cfg(feature = "bigtable")]
85 DbError::BTError(e) => e.metric_label(),
86 DbError::Backoff(_) => Some("storage.error.backoff"),
87 _ => None,
88 }
89 }
90
91 fn extras(&self) -> Vec<(&str, String)> {
92 match &self {
93 #[cfg(feature = "bigtable")]
94 DbError::BTError(e) => e.extras(),
95 DbError::Backoff(e) => {
96 vec![("raw", e.to_string())]
97 }
98 DbError::Integrity(_, Some(row)) => vec![("row", row.clone())],
99 _ => vec![],
100 }
101 }
102}