Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consider thiserror for errors #1

Open
kvark opened this issue Feb 23, 2021 · 0 comments
Open

Consider thiserror for errors #1

kvark opened this issue Feb 23, 2021 · 0 comments
Labels
good first issue Good for newcomers

Comments

@kvark
Copy link

kvark commented Feb 23, 2021

For example, this code:

#[derive(Debug)]
pub enum UpdateObjectError {
    SliceTooSmall,
    SliceToVanjectParse(VanjectError),
    PlayerNotFound(ClientID),
    VanjectNotFound(i32),
    PlayerNotBind(ClientID),
}

impl fmt::Display for UpdateObjectError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::SliceTooSmall => write!(f, "fail read slice as vanject: [too small slice]"),
            Self::SliceToVanjectParse(e) => write!(f, "fail read slice as vanject: [{}]", e),
            Self::PlayerNotFound(id) => write!(f, "player with `client_id`={} not found", id),
            Self::VanjectNotFound(id) => write!(f, "vanject with `id`={} not found", id),
            Self::PlayerNotBind(id) => write!(f, "player with `client_id`={} not bind", id),
        }
    }
}

impl From<UpdateObjectError> for OnUpdateError {
    fn from(from: UpdateObjectError) -> Self {
        Self::UpdateObjectError(from)
    }
}

would turn into

#[derive(Debug, thiserror::Error)]
pub enum UpdateObjectError {
    #[error("fail read slice as vanject: [too small slice]")]
    SliceTooSmall,
    #[error("fail read slice as vanject")]
    SliceToVanjectParse(#[from] VanjectError),
    #[error("player with `client_id`={0} not found")]
    PlayerNotFound(ClientID),
    #[error("vanject with `id`={0} not found")]
    VanjectNotFound(i32),
    #[error("player with `client_id`={0} not bind")]
    PlayerNotBind(ClientID),
}
@kvark kvark changed the title Consider thiserror for errors Consider thiserror for errors Feb 23, 2021
@Fenex Fenex added the good first issue Good for newcomers label Feb 24, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

2 participants