Gestion des erreurs avancée

Une bibliothèque définira plutôt ses propres types erreur, souvent avec thiserror :

use thiserror::Error;

/// Requested file path is not suitable for hwloc consumption
#[derive(Copy, Clone, Debug, Error, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub enum PathError {
    /// Path contains the NUL char, and is thus not compatible with C
    #[error("hwloc file paths can't contain NUL chars")]
    ContainsNul,

    /// Path contains non-Unicode data
    ///
    /// We need paths to be valid Unicode, even though most operating systems do
    /// not mandate it, because that is a prerequisite for portably converting
    /// paths to `char*` for C/hwloc consumption.
    #[error("hwloc file paths can't contain non-Unicode data")]
    NotUnicode,
}

Ca simplifie l’écriture d’applis avec une gestion d’erreurs plus fine, par exemple…

  • Se remettre de certaines erreurs (ex : réessayer si timeout, demander une correction…)
  • Adapter le message d’erreur à l’utilisateur (ex : traductions, enlever des infos sensibles…)