Section 13 of 25
Errors
Contents
- Every crate has its own error enum (with
thiserror). A slice may have its own inerror.rs. corereturns domain errors.app/<slice>/dto.rsturns the slice's errors into a code and a sentence key for the window, next to the slice's other wire types.app/error.rsholds onlyIpcErroritself, so a new slice adds its errors in its own folder, not in a shared file. The sentence comes from your translation table, never from theDisplayof a wire enum.- An unknown event from the wire is not an error. Use
#[serde(other)]on enums andOption<T>on fields that are not essential. Log the unknown line for debugging and move on. A new version of a provider's command-line tool must never stop a run. unwrap()andpanic!are forbidden outside tests inkit,store,driversandcore. Use?with an error variant, orexpect()with a reason that explains why the value cannot be missing.
#[derive(Debug, thiserror::Error)]
pub enum RecordingError {
#[error("the recorder could not start")]
RecorderDidNotStart(#[source] PlatformError),
#[error("the recording folder is not writable")]
FolderNotWritable(#[source] std::io::Error),
#[error("a recording is already running")]
AlreadyRunning,
}
app/src/error.rs:
#[derive(serde::Serialize)]
#[serde(rename_all = "camelCase")]
pub struct IpcError {
code: &'static str,
message_key: &'static str,
}
impl IpcError {
pub fn new(code: &'static str, message_key: &'static str) -> Self {
Self { code, message_key }
}
}
app/src/recording/dto.rs:
impl From<RecordingError> for IpcError {
fn from(error: RecordingError) -> Self {
match error {
RecordingError::RecorderDidNotStart(_) => Self::new("recorder", "errors.recorder"),
RecordingError::FolderNotWritable(_) => Self::new("folder", "errors.folderNotWritable"),
RecordingError::AlreadyRunning => Self::new("busy", "errors.recordingRunning"),
}
}
}