use rho_protocol::{ErrorCode, RpcError, TurnOutcome};
use rho_sdk::SessionId;
use super::super::jobs::JobId;
pub(super) fn writing(job: &JobId, session: &SessionId) -> RpcError {
RpcError::new(
ErrorCode::SessionBusy,
format!("job {job} is writing session {session}"),
)
}
pub(super) fn being_deleted() -> RpcError {
RpcError::new(ErrorCode::SessionBusy, "the session is being deleted")
}
pub(super) fn stopping() -> RpcError {
RpcError::new(ErrorCode::DaemonStopping, "the daemon is stopping")
}
pub(super) fn ended_early(outcome: &TurnOutcome) -> RpcError {
let message = match outcome {
TurnOutcome::Failed { error } => error.message.clone(),
TurnOutcome::Cancelled => "the turn was cancelled before it started".to_owned(),
TurnOutcome::TimedOut { timeout_ms } => {
format!("the turn reached its {timeout_ms}ms time limit before it started")
}
_ => "the turn ended before it started".to_owned(),
};
let data = serde_json::to_value(outcome).expect("a turn outcome serializes");
RpcError::new(ErrorCode::TurnEnded, message).with_data(data)
}