Section 13 of 25

Errors

Contents
  • Every crate has its own error enum (with thiserror). A slice may have its own in error.rs.
  • core returns domain errors. app/<slice>/dto.rs turns the slice's errors into a code and a sentence key for the window, next to the slice's other wire types. app/error.rs holds only IpcError itself, 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 the Display of a wire enum.
  • An unknown event from the wire is not an error. Use #[serde(other)] on enums and Option<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() and panic! are forbidden outside tests in kit, store, drivers and core. Use ? with an error variant, or expect() 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"),
        }
    }
}