1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
use axum::http::StatusCode;
use axum::response::{IntoResponse, Response};
use miette::Diagnostic;
#[derive(Debug, thiserror::Error, Diagnostic)]
pub enum Error {
#[error("io error: {0}")]
#[diagnostic(code(docket::io))]
Io(#[from] std::io::Error),
#[error("database error: {0}")]
#[diagnostic(code(docket::db))]
Db(#[from] rusqlite::Error),
#[error("jmap error: {0}")]
#[diagnostic(code(docket::jmap))]
Jmap(#[from] crate::jmap::JmapError),
#[error("no user identity on the request")]
#[diagnostic(code(docket::unauthenticated))]
Unauthenticated,
#[error("no such {0}")]
#[diagnostic(code(docket::not_found))]
NotFound(&'static str),
#[error("{0}")]
#[diagnostic(code(docket::forbidden))]
Forbidden(&'static str),
#[error("{0}")]
#[diagnostic(code(docket::bad_request))]
BadRequest(&'static str),
}
impl IntoResponse for Error {
fn into_response(self) -> Response {
let status = match self {
Error::Io(_) | Error::Db(_) | Error::Jmap(_) => StatusCode::INTERNAL_SERVER_ERROR,
Error::Unauthenticated => StatusCode::UNAUTHORIZED,
Error::NotFound(_) => StatusCode::NOT_FOUND,
Error::Forbidden(_) => StatusCode::FORBIDDEN,
Error::BadRequest(_) => StatusCode::BAD_REQUEST,
};
// A 500 is Docket's fault, so it goes to Sentry as an issue;
// the rest are the request's.
if status.is_server_error() {
tracing::error!(err = %self, "request failed");
}
(status, self.to_string()).into_response()
}
}
impl Error {
/// Whether a background job hitting this should try again rather
/// than stop.
pub fn is_retryable(&self) -> bool {
match self {
Error::Jmap(err) => err.is_retryable(),
Error::Db(err) => matches!(
err.sqlite_error_code(),
Some(rusqlite::ErrorCode::DatabaseBusy | rusqlite::ErrorCode::DatabaseLocked)
),
Error::Io(_)
| Error::Unauthenticated
| Error::NotFound(_)
| Error::Forbidden(_)
| Error::BadRequest(_) => false,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn statuses() {
let cases = [
(Error::Io(std::io::Error::other("disk")), 500),
(Error::Db(rusqlite::Error::QueryReturnedNoRows), 500),
(
Error::Jmap(crate::jmap::JmapError::NoMailAccount {
credential: "household".into(),
}),
500,
),
(Error::Unauthenticated, 401),
(Error::NotFound("thread"), 404),
(Error::Forbidden("read-only"), 403),
(Error::BadRequest("empty"), 400),
];
for (err, status) in cases {
assert_eq!(err.into_response().status().as_u16(), status);
}
}
#[test]
fn only_contention_and_transient_jmap_failures_retry() {
let sqlite = |code| {
Error::Db(rusqlite::Error::SqliteFailure(
rusqlite::ffi::Error::new(code),
None,
))
};
assert!(sqlite(rusqlite::ffi::SQLITE_BUSY).is_retryable());
assert!(sqlite(rusqlite::ffi::SQLITE_LOCKED).is_retryable());
assert!(!sqlite(rusqlite::ffi::SQLITE_CORRUPT).is_retryable());
assert!(!Error::Db(rusqlite::Error::QueryReturnedNoRows).is_retryable());
let method = |kind: &str| {
Error::Jmap(crate::jmap::JmapError::Method {
method: "Email/get",
kind: kind.into(),
description: None,
})
};
assert!(method("serverUnavailable").is_retryable());
assert!(!method("invalidArguments").is_retryable());
for err in [
Error::Io(std::io::Error::other("disk")),
Error::Unauthenticated,
Error::NotFound("thread"),
Error::Forbidden("read-only"),
Error::BadRequest("empty"),
] {
assert!(!err.is_retryable(), "{err}");
}
}
}