Skip to content

Commit e7365f2

Browse files
authored
Add error variant name rule (#529)
* Add error variant name rule * Adjust indentations for Rust code blocks
1 parent 16e81d5 commit e7365f2

File tree

1 file changed

+61
-24
lines changed

1 file changed

+61
-24
lines changed

modules/contributor/pages/code-style-guide.adoc

Lines changed: 61 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -246,25 +246,25 @@ The usage of `thiserror` is considered invalid.
246246
----
247247
#[derive(thiserror::Error)]
248248
enum Error {
249-
#[error("failed to read config file")]
250-
FileRead(#[from] std::io::Error)
249+
#[error("failed to read config file")]
250+
FileRead(#[from] std::io::Error)
251251
}
252252
253253
fn config_file(user: User) -> Result<(), Error> {
254-
std::fs::read_to_string(user.file_path)?;
254+
std::fs::read_to_string(user.file_path)?;
255255
}
256256
----
257257
258258
[source,rust]
259259
----
260260
#[derive(Snafu)]
261261
enum Error {
262-
#[snafu(context(false))]
263-
FileRead { source: std::io::Error }
262+
#[snafu(context(false))]
263+
FileRead { source: std::io::Error }
264264
}
265265
266266
fn config_file(user: User) -> Result<(), Error> {
267-
std::fs::read_to_string(user.file_path)?;
267+
std::fs::read_to_string(user.file_path)?;
268268
}
269269
----
270270
@@ -277,17 +277,54 @@ fn config_file(user: User) -> Result<(), Error> {
277277
----
278278
#[derive(Snafu)]
279279
enum Error {
280-
#[snafu(display("failed to read config file of user {user_name}"))]
281-
FileRead {
282-
source: std::io::Error,
283-
user_name: String,
284-
}
280+
#[snafu(display("failed to read config file of user {user_name}"))]
281+
FileRead {
282+
source: std::io::Error,
283+
user_name: String,
284+
}
285285
}
286286
287287
fn config_file(user: User) -> Result<(), Error> {
288-
std::fs::read_to_string(user.file_path).context(FileReadSnafu {
289-
user_name: user.name,
290-
});
288+
std::fs::read_to_string(user.file_path).context(FileReadSnafu {
289+
user_name: user.name,
290+
});
291+
}
292+
----
293+
294+
====
295+
296+
=== Error variant names
297+
298+
All error variants must not include any unnesecarry prefixes or suffixes.
299+
Examples of such prefixes include (but are not limited to) `FailedTo` and `UnableTo`.
300+
Furthermore, examples for suffixes are `Error` or `Snafu`
301+
Error variant names must however include verbs or identifiers as a prefix.
302+
303+
[WARNING.code-rule,caption=Examples of incorrect code for this rule]
304+
====
305+
306+
[source,rust]
307+
----
308+
#[derive(Snafu)]
309+
enum Error {
310+
FailedToParseConfig,
311+
HttpRequestError,
312+
ConfigRead,
313+
}
314+
----
315+
316+
====
317+
318+
[TIP.code-rule,caption=Examples of correct code for this rule]
319+
====
320+
321+
[source,rust]
322+
----
323+
#[derive(Snafu)]
324+
enum Error {
325+
ParseConfig,
326+
HttpRequest,
327+
ReadConfig,
291328
}
292329
----
293330
@@ -305,14 +342,14 @@ It is recommended to start the error messages with "failed to..." or "unable to
305342
----
306343
#[derive(Snafu)]
307344
enum Error {
308-
#[snafu(display("Foo happened."))]
309-
Foo,
345+
#[snafu(display("Foo happened."))]
346+
Foo,
310347
311-
#[snafu(display("Bar encountered"))]
312-
Bar,
348+
#[snafu(display("Bar encountered"))]
349+
Bar,
313350
314-
#[snafu(display("arghh baz."))]
315-
Baz,
351+
#[snafu(display("arghh baz."))]
352+
Baz,
316353
}
317354
----
318355
@@ -325,11 +362,11 @@ enum Error {
325362
----
326363
#[derive(Snafu)]
327364
enum Error {
328-
#[snafu(display("failed to foo"))]
329-
Foo,
365+
#[snafu(display("failed to foo"))]
366+
Foo,
330367
331-
#[snafu(display("unable to bar"))]
332-
Bar,
368+
#[snafu(display("unable to bar"))]
369+
Bar,
333370
}
334371
----
335372

0 commit comments

Comments
 (0)