Skip to content

Error handling: Error type #10

@dhardy

Description

@dhardy

This is about design of the error type; see also #8 (should we just panic?) and #9 (error kinds).

(This assumes we do return a Result somewhere and don't do all handling via panics.)

Possible error types:


Rustdoc: https://dhardy.github.io/rand/rand_core/struct.Error.html

New proposal, which tries to be uniform between std and no_std (aside from missing cause), and allow documentation of both the current error and its cause without creating a new type.

#[derive(Debug)]
pub struct Error {
    pub kind: ErrorKind,
    msg: &'static str,
    #[cfg(feature="std")]
    cause: Option<Box<stdError + Send + Sync>>,
}

impl Error {
    #[cfg(feature="std")]
    pub fn new<E>(kind: ErrorKind, msg: &'static str, cause: Option<E>) -> Self
        where E: Into<Box<stdError + Send + Sync>>
    {
        Self { kind, msg, cause: cause.map(|inner| inner.into()) }
    }

    #[cfg(not(feature="std"))]
    pub fn new<E>(kind: ErrorKind, msg: &'static str, _cause: Option<E>) -> Self {
        Self { kind, msg }
    }
}

Previous proposal:

#[cfg(feature="std")]
// impls: Debug, Display, ::std::error::Error
pub struct Error {
    pub kind: ErrorKind,
    pub cause: Option<Box<std::error::Error>,
}
#[cfg(not(feature="std"))]
// impls: Debug, Display, ::std::error::Error, Clone, PartialEq, Eq
pub struct Error {
    pub kind: ErrorKind,
    pub cause: Option<&'static str>,
}

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions