セクション 13 / 25
エラー
目次
- 各crateは独自のエラーenumを持ちます(
thiserrorを使います)。スライスはerror.rsに独自のものを持てます。 coreはドメインのエラーを返します。app/<slice>/dto.rsが、スライスのエラーをウィンドウ向けのコードと文のキーに変換します。置き場所は、そのスライスのほかのワイヤー型の隣です。app/error.rsが持つのはIpcErrorそのものだけです。だから新しいスライスは、自分のエラーを共有のファイルではなく自分のフォルダーに追加します。文は翻訳テーブルから取り、ワイヤー上のenumのDisplayからは決して取りません。- ワイヤーから届いた未知のイベントはエラーではありません。 enumには
#[serde(other)]を、必須でないフィールドにはOption<T>を使います。未知の行はデバッグ用にログに残し、先へ進みます。プロバイダーのコマンドラインツールの新しいバージョンが、実行を止めることがあってはなりません。 unwrap()とpanic!は禁止です。kit、store、drivers、coreのテスト以外の場所でのことです。エラーの変種を伴う?を使うか、値が欠けることのない理由を説明したexpect()を使います。
#[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"),
}
}
}