Reviewer · Error Handling
User-facing error messages
Write error messages for the person reading them, not for the implementation. Follow these rules: - Keep the tone matter-of-fact: lowercase the first word (no leading capital) and omit a trailing period unless multiple sentences are needed, matching rustc's diagnostic style.
Write error messages for the person reading them, not for the implementation. Follow these rules:
- Keep the tone matter-of-fact: lowercase the first word (no leading capital) and omit a trailing period unless multiple sentences are needed, matching rustc’s diagnostic style.
- Describe the problem and cause in terms the user understands, not internal mechanics. E.g. prefer “invalid
package.buildfile name” over a message that just restates the internal check, and prefer “unable to fetch dependency:" over exposing internal struct/variant names. - Never phrase hints as rhetorical questions like “did you mean: X?”. Instead state a fact, e.g. “a similar field exists:
workspace.lints” or “packages with similar names: …”. - When quoting back something the user wrote (a flag, a lint name, a path), preserve their original spelling/casing instead of an internally normalized form, so the message matches what they typed.
- Make sure the reported error kind matches the real cause; don’t reuse a specific error variant (e.g. “malformed protocol”) for a broader class of failures (e.g. generic URL parse errors) just because the code path is shared.
- Avoid duplicating the bulk of a message to add a conditional suffix; build an optional suggestion string and append it once:
let suggestion = if is_permission_denied { "\n\nNote: if this is a local artifact, add it to .gitignore or `exclude` in Cargo.toml." } else { "" }; anyhow::Error::from(e).context(format!( "failed to open for archiving: `{}`{}", disk_path.display(), suggestion, )) - Distinguish user errors from internal bugs: use a plain
bail!/format_err!for conditions the user caused (e.g. a missing manifest), and reserve the “internal error, please file a bug report” style only for cases that are truly unexpected bugs in Cargo itself; don’t let a user-facing condition fall through to the internal-bug message.
Applying these checks during review keeps diagnostics consistent, actionable, and trustworthy across the codebase.