aro-fletch 1.0.0

Fletch ORM persistence adapter for the Aro web framework
Documentation
//! Error conversion from fletch errors to aro-core domain errors.

use aro_core::error::RepoError;
use fletch_orm::FletchError;

/// Convert a [`FletchError`] into a [`RepoError`].
#[doc(hidden)]
pub fn from_fletch_err(err: FletchError) -> RepoError {
    match err {
        FletchError::NotFound => RepoError::NotFound,
        other if FletchError::is_unique_violation(&other) => RepoError::Conflict,
        other => RepoError::Unknown(Box::new(other)),
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn not_found_maps_to_not_found() {
        let repo_err = from_fletch_err(FletchError::NotFound);
        assert!(matches!(repo_err, RepoError::NotFound));
    }

    #[test]
    fn unique_violation_variant_maps_to_conflict() {
        let err = FletchError::UniqueViolation("duplicate key".into());
        let repo_err = from_fletch_err(err);
        assert!(matches!(repo_err, RepoError::Conflict));
    }

    #[test]
    fn sqlite_unique_violation_maps_to_conflict() {
        let err = FletchError::Database(
            "error returned from database: (code: 2067) UNIQUE constraint failed: users.email"
                .into(),
        );
        let repo_err = from_fletch_err(err);
        assert!(matches!(repo_err, RepoError::Conflict));
    }

    #[test]
    fn postgres_unique_violation_maps_to_conflict() {
        let err = FletchError::Database(
            "error returned from database: duplicate key value violates unique constraint \"users_email_key\""
                .into(),
        );
        let repo_err = from_fletch_err(err);
        assert!(matches!(repo_err, RepoError::Conflict));
    }

    #[test]
    fn mysql_unique_violation_maps_to_conflict() {
        let err = FletchError::Database(
            "error returned from database: Duplicate entry 'alice@example.com' for key 'users.email'"
                .into(),
        );
        let repo_err = from_fletch_err(err);
        assert!(matches!(repo_err, RepoError::Conflict));
    }

    #[test]
    fn database_error_maps_to_unknown() {
        let repo_err = from_fletch_err(FletchError::Database("connection refused".into()));
        assert!(matches!(repo_err, RepoError::Unknown(_)));
    }

    #[test]
    fn pool_closed_maps_to_unknown() {
        let repo_err = from_fletch_err(FletchError::PoolClosed);
        assert!(matches!(repo_err, RepoError::Unknown(_)));
    }
}