Skip to content

Commit c31cbb1

Browse files
authored
Merge pull request #9968 from Turbo87/read-only
Remove `ReadOnlyMode` error struct
2 parents 7788b02 + f8ee4b6 commit c31cbb1

File tree

4 files changed

+15
-29
lines changed

4 files changed

+15
-29
lines changed

src/controllers/user/session.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ use crate::middleware::session::SessionExtension;
1515
use crate::models::{NewUser, User};
1616
use crate::schema::users;
1717
use crate::tasks::spawn_blocking;
18-
use crate::util::diesel::Conn;
19-
use crate::util::errors::{bad_request, server_error, AppResult, BoxedAppError, ReadOnlyMode};
18+
use crate::util::diesel::{is_read_only_error, Conn};
19+
use crate::util::errors::{bad_request, server_error, AppResult};
2020
use crate::views::EncodableMe;
2121
use crates_io_github::GithubUser;
2222

@@ -145,11 +145,10 @@ fn save_user_to_database(
145145
access_token,
146146
)
147147
.create_or_update(user.email.as_deref(), emails, conn)
148-
.map_err(Into::into)
149-
.or_else(|e: BoxedAppError| {
148+
.or_else(|e| {
150149
// If we're in read only mode, we can't update their details
151150
// just look for an existing user
152-
if e.is::<ReadOnlyMode>() {
151+
if is_read_only_error(&e) {
153152
users::table
154153
.filter(users::gh_id.eq(user.id))
155154
.first(conn)
@@ -159,6 +158,7 @@ fn save_user_to_database(
159158
Err(e)
160159
}
161160
})
161+
.map_err(Into::into)
162162
}
163163

164164
/// Handles the `DELETE /api/private/session` route.

src/util/diesel.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
use diesel::connection::LoadConnection;
22
use diesel::pg::Pg;
3+
use diesel::result::Error;
34

45
pub trait Conn: LoadConnection<Backend = Pg> {}
56

67
impl<T> Conn for T where T: LoadConnection<Backend = Pg> {}
78

9+
pub fn is_read_only_error(error: &Error) -> bool {
10+
matches!(error, Error::DatabaseError(_, info) if info.message().ends_with("read-only transaction"))
11+
}
12+
813
pub mod prelude {
914
//! Inline diesel prelude
1015
pub use diesel::associations::{Associations, GroupedBy, Identifiable};

src/util/errors.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,10 @@ use crate::middleware::log_request::ErrorField;
3030
mod json;
3131

3232
use crate::email::EmailError;
33+
use crate::util::diesel::is_read_only_error;
3334
use crates_io_github::GitHubError;
3435
pub use json::TOKEN_FORMAT_ERROR;
35-
pub(crate) use json::{custom, InsecurelyGeneratedTokenRevoked, ReadOnlyMode, TooManyRequests};
36+
pub(crate) use json::{custom, InsecurelyGeneratedTokenRevoked, TooManyRequests};
3637

3738
pub type BoxedAppError = Box<dyn AppError>;
3839

@@ -145,10 +146,9 @@ impl From<DieselError> for BoxedAppError {
145146
fn from(err: DieselError) -> BoxedAppError {
146147
match err {
147148
DieselError::NotFound => not_found(),
148-
DieselError::DatabaseError(_, info)
149-
if info.message().ends_with("read-only transaction") =>
150-
{
151-
Box::new(ReadOnlyMode)
149+
e if is_read_only_error(&e) => {
150+
let detail = "crates.io is currently in read-only mode for maintenance. Please try again later.";
151+
custom(StatusCode::SERVICE_UNAVAILABLE, detail)
152152
}
153153
DieselError::DatabaseError(DatabaseErrorKind::ClosedConnection, _) => {
154154
service_unavailable()

src/util/errors/json.rs

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,25 +17,6 @@ fn json_error(detail: &str, status: StatusCode) -> Response {
1717
(status, json).into_response()
1818
}
1919

20-
// The following structs are empty and do not provide a custom message to the user
21-
22-
#[derive(Debug)]
23-
pub(crate) struct ReadOnlyMode;
24-
25-
impl AppError for ReadOnlyMode {
26-
fn response(&self) -> Response {
27-
let detail = "crates.io is currently in read-only mode for maintenance. \
28-
Please try again later.";
29-
json_error(detail, StatusCode::SERVICE_UNAVAILABLE)
30-
}
31-
}
32-
33-
impl fmt::Display for ReadOnlyMode {
34-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
35-
"Tried to write in read only mode".fmt(f)
36-
}
37-
}
38-
3920
// The following structs wrap owned data and provide a custom message to the user
4021

4122
pub fn custom(status: StatusCode, detail: impl Into<Cow<'static, str>>) -> BoxedAppError {

0 commit comments

Comments
 (0)