Skip to content

Add error variant name rule #529

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

Merged
merged 2 commits into from
Jan 17, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 61 additions & 24 deletions modules/contributor/pages/code-style-guide.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -246,25 +246,25 @@ The usage of `thiserror` is considered invalid.
----
#[derive(thiserror::Error)]
enum Error {
#[error("failed to read config file")]
FileRead(#[from] std::io::Error)
#[error("failed to read config file")]
FileRead(#[from] std::io::Error)
}

fn config_file(user: User) -> Result<(), Error> {
std::fs::read_to_string(user.file_path)?;
std::fs::read_to_string(user.file_path)?;
}
----

[source,rust]
----
#[derive(Snafu)]
enum Error {
#[snafu(context(false))]
FileRead { source: std::io::Error }
#[snafu(context(false))]
FileRead { source: std::io::Error }
}

fn config_file(user: User) -> Result<(), Error> {
std::fs::read_to_string(user.file_path)?;
std::fs::read_to_string(user.file_path)?;
}
----

Expand All @@ -277,17 +277,54 @@ fn config_file(user: User) -> Result<(), Error> {
----
#[derive(Snafu)]
enum Error {
#[snafu(display("failed to read config file of user {user_name}"))]
FileRead {
source: std::io::Error,
user_name: String,
}
#[snafu(display("failed to read config file of user {user_name}"))]
FileRead {
source: std::io::Error,
user_name: String,
}
}

fn config_file(user: User) -> Result<(), Error> {
std::fs::read_to_string(user.file_path).context(FileReadSnafu {
user_name: user.name,
});
std::fs::read_to_string(user.file_path).context(FileReadSnafu {
user_name: user.name,
});
}
----

====

=== Error variant names

All error variants must not include any unnesecarry prefixes or suffixes.
Examples of such prefixes include (but are not limited to) `FailedTo` and `UnableTo`.
Furthermore, examples for suffixes are `Error` or `Snafu`
Error variant names must however include verbs or identifiers as a prefix.

[WARNING.code-rule,caption=Examples of incorrect code for this rule]
====

[source,rust]
----
#[derive(Snafu)]
enum Error {
FailedToParseConfig,
HttpRequestError,
ConfigRead,
}
----

====

[TIP.code-rule,caption=Examples of correct code for this rule]
====

[source,rust]
----
#[derive(Snafu)]
enum Error {
ParseConfig,
HttpRequest,
ReadConfig,
}
----

Expand All @@ -305,14 +342,14 @@ It is recommended to start the error messages with "failed to..." or "unable to
----
#[derive(Snafu)]
enum Error {
#[snafu(display("Foo happened."))]
Foo,
#[snafu(display("Foo happened."))]
Foo,

#[snafu(display("Bar encountered"))]
Bar,
#[snafu(display("Bar encountered"))]
Bar,

#[snafu(display("arghh baz."))]
Baz,
#[snafu(display("arghh baz."))]
Baz,
}
----

Expand All @@ -325,11 +362,11 @@ enum Error {
----
#[derive(Snafu)]
enum Error {
#[snafu(display("failed to foo"))]
Foo,
#[snafu(display("failed to foo"))]
Foo,

#[snafu(display("unable to bar"))]
Bar,
#[snafu(display("unable to bar"))]
Bar,
}
----

Expand Down