Lines
100 %
Functions
Branches
//! The one place a command turns a failed connection into its error.
use rust_i18n::t;
use sqlx::{Postgres, pool::PoolConnection};
use crate::command::CmdError;
use crate::db::{DBError, get_connection};
use crate::user::{TenantConnection, User};
/// Logs an unreachable database here, since callers answer it without a log of their own.
/// Any other fault is left to the caller that handles it, so each error logs once.
fn refused(err: DBError) -> CmdError {
if err.is_unavailable() {
tracing::error!("{}", t!("Database error: %{err}", err = err : {:?}));
}
err.into()
/// The user's tenant connection.
///
/// # Errors
/// [`CmdError::Server`] carrying the [`DBError`] that refused the connection.
pub(crate) async fn connection(user: &User) -> Result<TenantConnection, CmdError> {
user.get_connection().await.map_err(refused)
/// A connection to the global directory.
pub(crate) async fn admin_connection() -> Result<PoolConnection<Postgres>, CmdError> {
get_connection().await.map_err(refused)
#[cfg(test)]
#[path = "connect_tests.rs"]
mod tests;