use std::fmt;
use std::process::ExitStatus;
use plughost_core::messages::CHAIN_NOT_PREPARED;
use plughost_core::{Failure, FailureKind, InputError, RenderError};
const HELPER_START: &str = "could not start the helper";
const HANDSHAKE: &str = "the helper did not identify itself with the expected token";
const PROTOCOL_VERSION: &str =
"the helper was built with another plughost version: its protocol is";
const APPLICATION_PROTOCOL: &str = "the application's is";
const CRASHED: &str = "the helper exited";
const TIMED_OUT: &str = "the helper stopped responding and was terminated";
const CANCELLED: &str = "the call was cancelled and the helper was terminated";
const OPERATION: &str = "the operation failed";
pub(crate) const REMOTE_UNSUPPORTED: &str = "remote mode is available on macOS only";
const REMOTE_BACKLOG: &str =
"the remote session's commands back up: the helper does not take them as fast as they come";
const REMOTE_CHANNEL: &str = "could not make the remote session's channel";
const REMOTE_SESSION_ENDED: &str =
"the remote session ended: its helper exited or a later session replaced it";
const PROTOCOL: &str = "the helper sent an invalid or unexpected message";
const SLOT: &str = "slot";
const CACHE: &str = "could not access the scan cache";
const INPUT: &str = "invalid input";
const RENDER: &str = "rendering failed";
const REALTIME_RUNNING: &str =
"the operation is refused while the chain's realtime endpoint runs; stop it first";
#[derive(Debug)]
#[non_exhaustive]
pub enum Error {
HelperStart(std::io::Error),
Handshake,
ProtocolVersion {
application: u32,
helper: u32,
},
Crashed {
slot: Option<usize>,
status: Option<ExitStatus>,
},
TimedOut {
slot: Option<usize>,
},
Cancelled {
slot: Option<usize>,
},
Operation {
slot: Option<usize>,
failure: Failure,
},
Protocol,
NotPrepared,
Input {
slot: Option<usize>,
error: InputError,
},
Render(RenderError),
Cache(std::io::Error),
RealtimeRunning,
RemoteSessionEnded,
RemoteBacklog,
RemoteChannel(std::io::Error),
}
fn slot(f: &mut fmt::Formatter<'_>, slot: &Option<usize>) -> fmt::Result {
match slot {
Some(slot) => write!(f, " ({SLOT} {slot})"),
None => Ok(()),
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Error::HelperStart(_) => f.write_str(HELPER_START),
Error::Handshake => f.write_str(HANDSHAKE),
Error::ProtocolVersion {
application,
helper,
} => write!(
f,
"{PROTOCOL_VERSION} {helper}, {APPLICATION_PROTOCOL} {application}"
),
Error::Crashed {
slot: which,
status,
} => {
f.write_str(CRASHED)?;
slot(f, which)?;
match status {
Some(status) => write!(f, ": {status}"),
None => Ok(()),
}
}
Error::TimedOut { slot: which } => {
f.write_str(TIMED_OUT)?;
slot(f, which)
}
Error::Cancelled { slot: which } => {
f.write_str(CANCELLED)?;
slot(f, which)
}
Error::Operation { slot: which, .. } => {
f.write_str(OPERATION)?;
slot(f, which)
}
Error::Protocol => f.write_str(PROTOCOL),
Error::NotPrepared => f.write_str(CHAIN_NOT_PREPARED),
Error::Input { slot: which, .. } => {
f.write_str(INPUT)?;
slot(f, which)
}
Error::Render(_) => f.write_str(RENDER),
Error::Cache(_) => f.write_str(CACHE),
Error::RealtimeRunning => f.write_str(REALTIME_RUNNING),
Error::RemoteSessionEnded => f.write_str(REMOTE_SESSION_ENDED),
Error::RemoteBacklog => f.write_str(REMOTE_BACKLOG),
Error::RemoteChannel(_) => f.write_str(REMOTE_CHANNEL),
}
}
}
impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Error::HelperStart(error) | Error::Cache(error) | Error::RemoteChannel(error) => {
Some(error)
}
Error::Operation { failure, .. } => Some(failure),
Error::Input { error, .. } => Some(error),
Error::Render(error) => Some(error),
_ => None,
}
}
}
impl From<RenderError> for Error {
fn from(error: RenderError) -> Error {
match error {
RenderError::Input(error) => Error::Input { slot: None, error },
error => Error::Render(error),
}
}
}
impl Error {
pub(crate) fn diagnostic(&self) -> String {
let mut message = self.to_string();
let mut cause = std::error::Error::source(self);
while let Some(error) = cause {
message.push_str(": ");
message.push_str(&error.to_string());
cause = error.source();
}
message
}
pub fn kind(&self) -> FailureKind {
match self {
Self::HelperStart(_)
| Self::Cache(_)
| Self::RemoteSessionEnded
| Self::RemoteBacklog
| Self::RemoteChannel(_) => FailureKind::Host,
Self::Handshake | Self::ProtocolVersion { .. } | Self::Protocol => {
FailureKind::Protocol
}
Self::Crashed { .. } => FailureKind::Crashed,
Self::TimedOut { .. } => FailureKind::TimedOut,
Self::Cancelled { .. } => FailureKind::Cancelled,
Self::Operation { failure, .. } => failure.kind,
Self::NotPrepared => FailureKind::NotPrepared,
Self::Input { .. } | Self::RealtimeRunning => FailureKind::InvalidInput,
Self::Render(error) => error.kind(),
}
}
}