Skip to content

Commit

Permalink
fixup! editoast: define Model error type and use it in trait Delete
Browse files Browse the repository at this point in the history
Signed-off-by: Leo Valais <leo.valais97@gmail.com>
  • Loading branch information
leovalais committed Jan 8, 2025
1 parent 2d7acaa commit 1ed77d0
Showing 1 changed file with 24 additions and 29 deletions.
53 changes: 24 additions & 29 deletions editoast/editoast_models/src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,41 +18,36 @@ pub enum Error {

impl From<diesel::result::Error> for Error {
fn from(e: diesel::result::Error) -> Self {
match &e {
diesel::result::Error::DatabaseError(DatabaseErrorKind::UniqueViolation, inner) => {
static RE: LazyLock<Regex> = LazyLock::new(|| {
Regex::new(
r#"duplicate key value violates unique constraint "([0-9a-zA-Z_-]+)""#,
)
if let diesel::result::Error::DatabaseError(DatabaseErrorKind::UniqueViolation, inner) = &e
{
static RE: LazyLock<Regex> = LazyLock::new(|| {
Regex::new(r#"duplicate key value violates unique constraint "([0-9a-zA-Z_-]+)""#)
.unwrap()
});
if let Some(captures) = RE.captures((*inner).message()) {
Self::UniqueViolation {
constraint: captures.get(1).unwrap().as_str().to_string(),
}
} else {
tracing::error!(?RE, %e, "failed to parse PostgreSQL error message");
Self::DatabaseError(e.into())
}
});
if let Some(captures) = RE.captures((*inner).message()) {
return Self::UniqueViolation {
constraint: captures.get(1).unwrap().as_str().to_string(),
};
} else {
tracing::error!(?RE, %e, "failed to parse PostgreSQL error message");
}
diesel::result::Error::DatabaseError(DatabaseErrorKind::CheckViolation, inner) => {
static RE: LazyLock<Regex> = LazyLock::new(|| {
Regex::new(
}
if let diesel::result::Error::DatabaseError(DatabaseErrorKind::CheckViolation, inner) = &e {
static RE: LazyLock<Regex> = LazyLock::new(|| {
Regex::new(
r#"new row for relation "([0-9a-zA-Z_-]+)" violates check constraint "([0-9a-zA-Z_-]+)""#,
)
.unwrap()
});
if let Some(captures) = RE.captures((*inner).message()) {
Self::CheckViolation {
relation: captures.get(1).unwrap().as_str().to_string(),
constraint: captures.get(2).unwrap().as_str().to_string(),
}
} else {
tracing::error!(?RE, %e, "failed to parse PostgreSQL error message");
Self::DatabaseError(e.into())
}
});
if let Some(captures) = RE.captures((*inner).message()) {
return Self::CheckViolation {
relation: captures.get(1).unwrap().as_str().to_string(),
constraint: captures.get(2).unwrap().as_str().to_string(),
};
} else {
tracing::error!(?RE, %e, "failed to parse PostgreSQL error message");
}
_ => Self::DatabaseError(e.into()),
}
Self::DatabaseError(e.into())
}
}

0 comments on commit 1ed77d0

Please sign in to comment.