Rust - error handling with thiserror crate

less than 1 minute read

One of the most difficult decision to make on developing an application is how we can handle errors. The thiserror crate provide an easy way to handle errors for Rust application.

If we do not care of detailed error, anyhow can be one of candidate to consider too.
https://github.com/dtolnay/anyhow

1
2
[dependencies]
thiserror = "1.0.20"

Defining an error enum.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
use thiserror::Error;
#[derive(Debug, Error)]
pub enum Error {
    #[error(transparent)]
    ImageError(image::error::ImageError),

    #[error("not valid base64 image string")]
    NotValidBase64,
    #[error("failed to convert an image")]
    ImageConvertFailure,
    #[error("out of index on finding gaussian")]
    GaussianIndexError,

    #[error("failed to create a color quantization")]
    FailureColorQuantization,

    #[error("failed to generate a palette")]
    FailureGeneratePallette,

    #[error("unknown error")]
    Unknown,
}

Categories:

Updated: