Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 27 additions & 8 deletions src/assignment/backends/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,17 @@
//
// SPDX-License-Identifier: Apache-2.0

use sea_orm::SqlErr;
use thiserror::Error;

use crate::assignment::types::*;

#[derive(Error, Debug)]
pub enum AssignmentDatabaseError {
#[error("role {0} not found")]
#[error("{0}")]
RoleNotFound(String),

#[error("data serialization error: {}", source)]
#[error(transparent)]
Serde {
#[from]
source: serde_json::Error,
Expand All @@ -39,12 +40,30 @@ pub enum AssignmentDatabaseError {
source: RoleBuilderError,
},

#[error("database error: {}", source)]
Database {
#[from]
source: sea_orm::DbErr,
},
#[error(transparent)]
Database { source: sea_orm::DbErr },

/// Conflict
#[error("{0}")]
Conflict(String),

#[error("invalid assignment type: {0}")]
/// SqlError
#[error("{0}")]
Sql(String),

#[error("{0}")]
InvalidAssignmentType(String),
}

impl From<sea_orm::DbErr> for AssignmentDatabaseError {
fn from(err: sea_orm::DbErr) -> Self {
match err.sql_err() {
Some(err) => match err {
SqlErr::UniqueConstraintViolation(descr) => Self::Conflict(descr),
SqlErr::ForeignKeyConstraintViolation(descr) => Self::Conflict(descr),
other => Self::Sql(other.to_string()),
},
None => Self::Database { source: err },
}
}
}
17 changes: 10 additions & 7 deletions src/assignment/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,16 @@ pub enum AssignmentProviderError {
source: serde_json::Error,
},

/// Conflict.
#[error("conflict: {0}")]
Conflict(String),

#[error("role {0} not found")]
RoleNotFound(String),

/// Assignment provider error
#[error("assignment provider database error: {}", source)]
AssignmentDatabaseError {
#[from]
source: AssignmentDatabaseError,
},
#[error(transparent)]
AssignmentDatabaseError { source: AssignmentDatabaseError },

/// Identity provider error
#[error(transparent)]
Expand Down Expand Up @@ -68,10 +69,12 @@ pub enum AssignmentProviderError {
},
}

impl AssignmentProviderError {
pub fn database(source: AssignmentDatabaseError) -> Self {
impl From<AssignmentDatabaseError> for AssignmentProviderError {
fn from(source: AssignmentDatabaseError) -> Self {
match source {
AssignmentDatabaseError::Conflict(x) => Self::Conflict(x),
AssignmentDatabaseError::RoleNotFound(x) => Self::RoleNotFound(x),
AssignmentDatabaseError::Serde { source } => Self::Serde { source },
_ => Self::AssignmentDatabaseError { source },
}
}
Expand Down
14 changes: 7 additions & 7 deletions src/federation/backends/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,30 +19,30 @@ use crate::federation::types::*;

#[derive(Error, Debug)]
pub enum FederationDatabaseError {
#[error("data serialization error")]
#[error(transparent)]
Serde {
#[from]
source: serde_json::Error,
},

#[error("database error")]
#[error(transparent)]
Database { source: sea_orm::DbErr },

#[error("identity provider {0} not found")]
#[error("{0}")]
IdentityProviderNotFound(String),

#[error("mapping provider {0} not found")]
#[error("{0}")]
MappingNotFound(String),

#[error("auth state {0} not found")]
#[error("{0}")]
AuthStateNotFound(String),

/// Conflict
#[error("conflict: {0}")]
#[error("{0}")]
Conflict(String),

/// SqlError
#[error("sql error: {0}")]
#[error("{0}")]
Sql(String),

#[error(transparent)]
Expand Down
3 changes: 2 additions & 1 deletion src/federation/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ pub enum FederationProviderError {
MappingTokenProjectDomainUnset,

/// Conflict.
#[error("oha {0}")]
#[error("conflict: {0}")]
Conflict(String),

/// Identity provider error.
Expand All @@ -62,6 +62,7 @@ impl From<FederationDatabaseError> for FederationProviderError {
}
FederationDatabaseError::MappingNotFound(x) => Self::MappingNotFound(x),
FederationDatabaseError::Conflict(x) => Self::Conflict(x),
FederationDatabaseError::Serde { source } => Self::Serde { source },
_ => Self::FederationDatabase { source },
}
}
Expand Down
35 changes: 27 additions & 8 deletions src/identity/backends/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
//
// SPDX-License-Identifier: Apache-2.0

use sea_orm::SqlErr;
use thiserror::Error;

use crate::identity::error::IdentityProviderPasswordHashError;
Expand All @@ -22,13 +23,13 @@ pub enum IdentityDatabaseError {
#[error("corrupted database entries for user {0}")]
MalformedUser(String),

#[error("user {0} not found")]
#[error("{0}")]
UserNotFound(String),

#[error("group {0} not found")]
#[error("{0}")]
GroupNotFound(String),

#[error("data serialization error")]
#[error(transparent)]
Serde {
#[from]
source: serde_json::Error,
Expand All @@ -46,11 +47,16 @@ pub enum IdentityDatabaseError {
source: FederationBuilderError,
},

#[error("database data")]
Database {
#[from]
source: sea_orm::DbErr,
},
/// Conflict
#[error("{0}")]
Conflict(String),

/// SqlError
#[error("{0}")]
Sql(String),

#[error(transparent)]
Database { source: sea_orm::DbErr },

#[error("password hashing error")]
PasswordHash {
Expand All @@ -61,3 +67,16 @@ pub enum IdentityDatabaseError {
#[error("either user id or user name with user domain id or name must be given")]
UserIdOrNameWithDomain,
}

impl From<sea_orm::DbErr> for IdentityDatabaseError {
fn from(err: sea_orm::DbErr) -> Self {
match err.sql_err() {
Some(err) => match err {
SqlErr::UniqueConstraintViolation(descr) => Self::Conflict(descr),
SqlErr::ForeignKeyConstraintViolation(descr) => Self::Conflict(descr),
other => Self::Sql(other.to_string()),
},
None => Self::Database { source: err },
}
}
}
16 changes: 4 additions & 12 deletions src/identity/backends/sql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,7 @@ impl IdentityBackend for SqlBackend {
db: &DatabaseConnection,
user_id: &'a str,
) -> Result<(), IdentityProviderError> {
user::delete(&self.config, db, user_id)
.await
.map_err(IdentityProviderError::database)
Ok(user::delete(&self.config, db, user_id).await?)
}

/// List groups
Expand Down Expand Up @@ -196,9 +194,7 @@ impl IdentityBackend for SqlBackend {
db: &DatabaseConnection,
group_id: &'a str,
) -> Result<(), IdentityProviderError> {
group::delete(&self.config, db, group_id)
.await
.map_err(IdentityProviderError::database)
Ok(group::delete(&self.config, db, group_id).await?)
}

/// List groups a user is member of
Expand Down Expand Up @@ -281,9 +277,7 @@ impl IdentityBackend for SqlBackend {
db: &DatabaseConnection,
user_id: &'a str,
) -> Result<(), IdentityProviderError> {
passkey_state::delete(db, user_id)
.await
.map_err(IdentityProviderError::database)
Ok(passkey_state::delete(db, user_id).await?)
}

/// Delete passkey auth state for a user
Expand All @@ -293,9 +287,7 @@ impl IdentityBackend for SqlBackend {
db: &DatabaseConnection,
user_id: &'a str,
) -> Result<(), IdentityProviderError> {
passkey_state::delete(db, user_id)
.await
.map_err(IdentityProviderError::database)
Ok(passkey_state::delete(db, user_id).await?)
}
}

Expand Down
15 changes: 9 additions & 6 deletions src/identity/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,7 @@ pub enum IdentityProviderError {

/// Identity provider error
#[error(transparent)]
IdentityDatabase {
#[from]
source: IdentityDatabaseError,
},
IdentityDatabase { source: IdentityDatabaseError },

#[error(transparent)]
UserBuilder {
Expand Down Expand Up @@ -90,15 +87,21 @@ pub enum IdentityProviderError {
source: ResourceProviderError,
},

/// Conflict.
#[error("conflict: {0}")]
Conflict(String),

#[error("wrong username or password")]
WrongUsernamePassword,
}

impl IdentityProviderError {
pub fn database(source: IdentityDatabaseError) -> Self {
impl From<IdentityDatabaseError> for IdentityProviderError {
fn from(source: IdentityDatabaseError) -> Self {
match source {
IdentityDatabaseError::Conflict(x) => Self::Conflict(x),
IdentityDatabaseError::UserNotFound(x) => Self::UserNotFound(x),
IdentityDatabaseError::GroupNotFound(x) => Self::GroupNotFound(x),
IdentityDatabaseError::Serde { source } => Self::Serde { source },
_ => Self::IdentityDatabase { source },
}
}
Expand Down
37 changes: 28 additions & 9 deletions src/resource/backends/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,36 +12,55 @@
//
// SPDX-License-Identifier: Apache-2.0

use sea_orm::SqlErr;
use thiserror::Error;

use crate::resource::types::*;

#[derive(Error, Debug)]
pub enum ResourceDatabaseError {
#[error("domain {0} not found")]
#[error("{0}")]
DomainNotFound(String),

#[error("data serialization error")]
#[error(transparent)]
Serde {
#[from]
source: serde_json::Error,
},

#[error("error building domain data")]
#[error(transparent)]
DomainBuilderError {
#[from]
source: DomainBuilderError,
},

#[error("error building project data")]
#[error(transparent)]
ProjectBuilderError {
#[from]
source: ProjectBuilderError,
},

#[error("database data")]
Database {
#[from]
source: sea_orm::DbErr,
},
/// Conflict
#[error("{0}")]
Conflict(String),

/// SqlError
#[error("{0}")]
Sql(String),

#[error(transparent)]
Database { source: sea_orm::DbErr },
}

impl From<sea_orm::DbErr> for ResourceDatabaseError {
fn from(err: sea_orm::DbErr) -> Self {
match err.sql_err() {
Some(err) => match err {
SqlErr::UniqueConstraintViolation(descr) => Self::Conflict(descr),
SqlErr::ForeignKeyConstraintViolation(descr) => Self::Conflict(descr),
other => Self::Sql(other.to_string()),
},
None => Self::Database { source: err },
}
}
}
Loading
Loading