Skip to content

Commit

Permalink
refactor: Don't use legacy numerics contant and methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Eragonfr committed Oct 29, 2024
1 parent c8ec7ca commit ea2dfaa
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 54 deletions.
42 changes: 19 additions & 23 deletions syncserver-db-common/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@ use http::StatusCode;
use syncserver_common::{from_error, impl_fmt_display, ReportableError};
use thiserror::Error;

/// Error specific to any MySQL database backend. These errors are not related to the syncstorage
/// Error specific to any SQL database backend. These errors are not related to the syncstorage
/// or tokenserver application logic; rather, they are lower-level errors arising from diesel.
#[derive(Debug)]
pub struct MysqlError {
kind: MysqlErrorKind,
pub struct SqlError {
kind: SqlErrorKind,
pub status: StatusCode,
pub backtrace: Backtrace,
}

#[derive(Debug, Error)]
enum MysqlErrorKind {
enum SqlErrorKind {
#[error("A database error occurred: {}", _0)]
DieselQuery(#[from] diesel::result::Error),

Expand All @@ -29,8 +29,8 @@ enum MysqlErrorKind {
Migration(diesel_migrations::RunMigrationsError),
}

impl From<MysqlErrorKind> for MysqlError {
fn from(kind: MysqlErrorKind) -> Self {
impl From<SqlErrorKind> for SqlError {
fn from(kind: SqlErrorKind) -> Self {
Self {
kind,
status: StatusCode::INTERNAL_SERVER_ERROR,
Expand All @@ -39,22 +39,22 @@ impl From<MysqlErrorKind> for MysqlError {
}
}

impl ReportableError for MysqlError {
impl ReportableError for SqlError {
fn is_sentry_event(&self) -> bool {
#[allow(clippy::match_like_matches_macro)]
match &self.kind {
MysqlErrorKind::Pool(_) => false,
SqlErrorKind::Pool(_) => false,
_ => true,
}
}

fn metric_label(&self) -> Option<String> {
Some(
match self.kind {
MysqlErrorKind::DieselQuery(_) => "diesel_query",
MysqlErrorKind::DieselConnection(_) => "diesel_connection",
MysqlErrorKind::Pool(_) => "pool",
MysqlErrorKind::Migration(_) => "migration",
SqlErrorKind::DieselQuery(_) => "diesel_query",
SqlErrorKind::DieselConnection(_) => "diesel_connection",
SqlErrorKind::Pool(_) => "pool",
SqlErrorKind::Migration(_) => "migration",
}
.to_string(),
)
Expand All @@ -65,21 +65,17 @@ impl ReportableError for MysqlError {
}
}

impl_fmt_display!(MysqlError, MysqlErrorKind);
impl_fmt_display!(SqlError, SqlErrorKind);

from_error!(
diesel::result::Error,
MysqlError,
MysqlErrorKind::DieselQuery
);
from_error!(diesel::result::Error, SqlError, SqlErrorKind::DieselQuery);
from_error!(
diesel::result::ConnectionError,
MysqlError,
MysqlErrorKind::DieselConnection
SqlError,
SqlErrorKind::DieselConnection
);
from_error!(diesel::r2d2::PoolError, MysqlError, MysqlErrorKind::Pool);
from_error!(diesel::r2d2::PoolError, SqlError, SqlErrorKind::Pool);
from_error!(
diesel_migrations::RunMigrationsError,
MysqlError,
MysqlErrorKind::Migration
SqlError,
SqlErrorKind::Migration
);
2 changes: 1 addition & 1 deletion syncstorage-db-common/src/util.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{convert::TryInto, u64};
use std::convert::TryInto;

use chrono::{
offset::{FixedOffset, TimeZone, Utc},
Expand Down
28 changes: 13 additions & 15 deletions syncstorage-mysql/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::fmt;
use backtrace::Backtrace;
use http::StatusCode;
use syncserver_common::{from_error, impl_fmt_display, InternalError, ReportableError};
use syncserver_db_common::error::MysqlError;
use syncserver_db_common::error::SqlError;
use syncstorage_db_common::error::{DbErrorIntrospect, SyncstorageDbError};
use thiserror::Error;

Expand Down Expand Up @@ -49,7 +49,7 @@ enum DbErrorKind {
Common(SyncstorageDbError),

#[error("{}", _0)]
Mysql(MysqlError),
Sql(SqlError),
}

impl From<DbErrorKind> for DbError {
Expand Down Expand Up @@ -95,35 +95,35 @@ impl ReportableError for DbError {
fn reportable_source(&self) -> Option<&(dyn ReportableError + 'static)> {
Some(match &self.kind {
DbErrorKind::Common(e) => e,
DbErrorKind::Mysql(e) => e,
DbErrorKind::Sql(e) => e,
})
}

fn is_sentry_event(&self) -> bool {
match &self.kind {
DbErrorKind::Common(e) => e.is_sentry_event(),
DbErrorKind::Mysql(e) => e.is_sentry_event(),
DbErrorKind::Sql(e) => e.is_sentry_event(),
}
}

fn metric_label(&self) -> Option<String> {
match &self.kind {
DbErrorKind::Common(e) => e.metric_label(),
DbErrorKind::Mysql(e) => e.metric_label(),
DbErrorKind::Sql(e) => e.metric_label(),
}
}

fn backtrace(&self) -> Option<&Backtrace> {
match &self.kind {
DbErrorKind::Common(e) => e.backtrace(),
DbErrorKind::Mysql(e) => e.backtrace(),
DbErrorKind::Sql(e) => e.backtrace(),
}
}

fn tags(&self) -> Vec<(&str, String)> {
match &self.kind {
DbErrorKind::Common(e) => e.tags(),
DbErrorKind::Mysql(e) => e.tags(),
DbErrorKind::Sql(e) => e.tags(),
}
}
}
Expand All @@ -140,24 +140,22 @@ from_error!(SyncstorageDbError, DbError, DbErrorKind::Common);
from_error!(
diesel::result::Error,
DbError,
|error: diesel::result::Error| DbError::from(DbErrorKind::Mysql(MysqlError::from(error)))
|error: diesel::result::Error| DbError::from(DbErrorKind::Sql(SqlError::from(error)))
);
from_error!(
diesel::result::ConnectionError,
DbError,
|error: diesel::result::ConnectionError| DbError::from(DbErrorKind::Mysql(MysqlError::from(
error
)))
|error: diesel::result::ConnectionError| DbError::from(DbErrorKind::Sql(SqlError::from(error)))
);
from_error!(
diesel::r2d2::PoolError,
DbError,
|error: diesel::r2d2::PoolError| DbError::from(DbErrorKind::Mysql(MysqlError::from(error)))
|error: diesel::r2d2::PoolError| DbError::from(DbErrorKind::Sql(SqlError::from(error)))
);
from_error!(
diesel_migrations::RunMigrationsError,
DbError,
|error: diesel_migrations::RunMigrationsError| DbError::from(DbErrorKind::Mysql(
MysqlError::from(error)
))
|error: diesel_migrations::RunMigrationsError| DbError::from(DbErrorKind::Sql(SqlError::from(
error
)))
);
2 changes: 1 addition & 1 deletion syncstorage-spanner/src/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1295,7 +1295,7 @@ impl SpannerDb {
query = format!(
"{} LIMIT {}",
query,
i64::max_value() - offset.offset as i64
i64::MAX - offset.offset as i64
);
};

Expand Down
26 changes: 12 additions & 14 deletions tokenserver-db/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::fmt;
use backtrace::Backtrace;
use http::StatusCode;
use syncserver_common::{from_error, impl_fmt_display, InternalError, ReportableError};
use syncserver_db_common::error::MysqlError;
use syncserver_db_common::error::SqlError;
use thiserror::Error;
use tokenserver_common::TokenserverError;

Expand All @@ -28,21 +28,21 @@ impl DbError {
impl ReportableError for DbError {
fn backtrace(&self) -> Option<&Backtrace> {
match &self.kind {
DbErrorKind::Mysql(e) => e.backtrace(),
DbErrorKind::Sql(e) => e.backtrace(),
_ => Some(&self.backtrace),
}
}

fn is_sentry_event(&self) -> bool {
match &self.kind {
DbErrorKind::Mysql(e) => e.is_sentry_event(),
DbErrorKind::Sql(e) => e.is_sentry_event(),
_ => true,
}
}

fn metric_label(&self) -> Option<String> {
match &self.kind {
DbErrorKind::Mysql(e) => e.metric_label(),
DbErrorKind::Sql(e) => e.metric_label(),
_ => None,
}
}
Expand All @@ -51,7 +51,7 @@ impl ReportableError for DbError {
#[derive(Debug, Error)]
enum DbErrorKind {
#[error("{}", _0)]
Mysql(MysqlError),
Sql(SqlError),

#[error("Unexpected error: {}", _0)]
Internal(String),
Expand All @@ -60,7 +60,7 @@ enum DbErrorKind {
impl From<DbErrorKind> for DbError {
fn from(kind: DbErrorKind) -> Self {
match kind {
DbErrorKind::Mysql(ref mysql_error) => Self {
DbErrorKind::Sql(ref mysql_error) => Self {
status: mysql_error.status,
backtrace: Box::new(mysql_error.backtrace.clone()),
kind,
Expand Down Expand Up @@ -105,24 +105,22 @@ impl_fmt_display!(DbError, DbErrorKind);
from_error!(
diesel::result::Error,
DbError,
|error: diesel::result::Error| DbError::from(DbErrorKind::Mysql(MysqlError::from(error)))
|error: diesel::result::Error| DbError::from(DbErrorKind::Sql(SqlError::from(error)))
);
from_error!(
diesel::result::ConnectionError,
DbError,
|error: diesel::result::ConnectionError| DbError::from(DbErrorKind::Mysql(MysqlError::from(
error
)))
|error: diesel::result::ConnectionError| DbError::from(DbErrorKind::Sql(SqlError::from(error)))
);
from_error!(
diesel::r2d2::PoolError,
DbError,
|error: diesel::r2d2::PoolError| DbError::from(DbErrorKind::Mysql(MysqlError::from(error)))
|error: diesel::r2d2::PoolError| DbError::from(DbErrorKind::Sql(SqlError::from(error)))
);
from_error!(
diesel_migrations::RunMigrationsError,
DbError,
|error: diesel_migrations::RunMigrationsError| DbError::from(DbErrorKind::Mysql(
MysqlError::from(error)
))
|error: diesel_migrations::RunMigrationsError| DbError::from(DbErrorKind::Sql(SqlError::from(
error
)))
);

0 comments on commit ea2dfaa

Please sign in to comment.