evidentsource-client 1.0.0-rc1

Rust client for the EvidentSource event sourcing platform
Documentation
//! Maps gRPC status codes to domain error types.
//!
//! This module reverses the server's error-to-status mappings, converting
//! `tonic::Status` codes back to domain errors.

use evidentsource_core::domain::{DatabaseError, StateChangeError, StateViewError};
use tonic::Code;

/// Convert a gRPC status to a DatabaseError.
pub fn to_database_error(status: &tonic::Status, name: &str) -> DatabaseError {
    match status.code() {
        Code::NotFound => DatabaseError::NotFound(name.to_string()),
        Code::AlreadyExists => DatabaseError::AlreadyExists(name.to_string()),
        Code::Aborted => DatabaseError::ConcurrentWriteCollision,
        Code::DeadlineExceeded => DatabaseError::Timeout,
        _ => DatabaseError::ServerError(format!("{:?}: {}", status.code(), status.message())),
    }
}

/// Convert a gRPC status to a StateViewError.
pub fn to_state_view_error(status: &tonic::Status, name: &str, version: u64) -> StateViewError {
    match status.code() {
        Code::NotFound => StateViewError::NotFound {
            name: name.to_string(),
            version,
        },
        _ => StateViewError::ServerError(format!("{:?}: {}", status.code(), status.message())),
    }
}

/// Convert a gRPC status to a StateChangeError.
pub fn to_state_change_error(status: &tonic::Status, name: &str, version: u64) -> StateChangeError {
    match status.code() {
        Code::NotFound => StateChangeError::NotFound {
            name: name.to_string(),
            version,
        },
        Code::FailedPrecondition => StateChangeError::ExecutionError(status.message().to_string()),
        _ => StateChangeError::ServerError(format!("{:?}: {}", status.code(), status.message())),
    }
}