1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
//! The session store, from a request: its blocking work run off the async
//! runtime, and its failures as the errors a client acts on.
use rho_protocol::{ErrorCode, RpcError};
use crate::session::{RunNotFinished, SavedInManyWorkspaces, SessionInUse, TitleRejected};
/// Runs session-store work `work`, which reads SQLite and walks session
/// directories, off the async runtime; its failure as [`store_failed`]
/// says, naming `what` it could not do.
pub(super) async fn store_blocking<T: Send + 'static>(
what: &'static str,
work: impl FnOnce() -> anyhow::Result<T> + Send + 'static,
) -> Result<T, RpcError> {
match tokio::task::spawn_blocking(work).await {
Ok(Ok(value)) => Ok(value),
Ok(Err(error)) => Err(store_failed(what, &error)),
Err(error) => Err(store_failed(what, &error.into())),
}
}
/// The error a request answers when the session store failed it with
/// `error`, wherever in its chain the store's refusal is, as a child
/// session's is under its parent's: a session something else holds is
/// `SessionInUse`, a run a delete must wait for `SessionBusy`, a refused
/// title or an id saved in more than one workspace `InvalidParams` (the
/// client chose them), and anything else `InternalError` saying the daemon
/// could not do `what`.
pub(super) fn store_failed(what: &str, error: &anyhow::Error) -> RpcError {
let refused = |cause: &&(dyn std::error::Error + 'static)| {
cause.is::<TitleRejected>() || cause.is::<SavedInManyWorkspaces>()
};
if error.chain().any(|cause| cause.is::<SessionInUse>()) {
return RpcError::new(ErrorCode::SessionInUse, format!("{error:#}"));
}
if let Some(run) = error
.chain()
.find_map(|cause| cause.downcast_ref::<RunNotFinished>())
{
// The store's own wording offers `rho sessions rm --force`, which
// the socket has no counterpart of.
return RpcError::new(
ErrorCode::SessionBusy,
format!(
"run {} of session {} is still {}; delete the session once the run ends",
run.run(),
run.session(),
run.state()
),
);
}
match error.chain().find(refused) {
Some(cause) => RpcError::new(ErrorCode::InvalidParams, cause.to_string()),
None => RpcError::new(
ErrorCode::InternalError,
format!("could not {what}: {error:#}"),
),
}
}