Skip to content

Commit 8e52700

Browse files
committed
feat: add miette diagnostic error reporting
Replace console::style error formatting in main.rs with miette's rich diagnostics. All error variants now carry diagnostic codes and help text guiding users to resolution steps.
1 parent 7d7d89d commit 8e52700

2 files changed

Lines changed: 54 additions & 12 deletions

File tree

src/error.rs

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,47 +2,80 @@
22
//
33
// SPDX-License-Identifier: GPL-3.0-only
44

5+
// miette's Diagnostic derive generates code that triggers this false positive
6+
#![allow(unused_assignments)]
7+
8+
use miette::Diagnostic;
59
use thiserror::Error;
610

7-
#[derive(Error, Debug)]
11+
#[derive(Error, Diagnostic, Debug)]
812
pub enum Error {
9-
#[error("No staged changes found. Use `git add` to stage files.")]
13+
#[error("No staged changes found")]
14+
#[diagnostic(code(commitbee::git::no_staged), help("Stage files with: git add <files>"))]
1015
NoStagedChanges,
1116

12-
#[error("Not a git repository. Run from a git project directory.")]
17+
#[error("Not a git repository")]
18+
#[diagnostic(
19+
code(commitbee::git::not_repo),
20+
help("Run this command inside a git repository")
21+
)]
1322
NotAGitRepo,
1423

15-
#[error("Merge conflicts detected. Resolve conflicts before committing.")]
24+
#[error("Merge conflicts detected")]
25+
#[diagnostic(
26+
code(commitbee::git::conflicts),
27+
help("Resolve conflicts before committing")
28+
)]
1629
MergeConflicts,
1730

18-
#[error("Merge in progress. Complete or abort the merge first.")]
31+
#[error("Merge in progress")]
32+
#[diagnostic(
33+
code(commitbee::git::merge),
34+
help("Complete or abort the merge: git merge --abort")
35+
)]
1936
MergeInProgress,
2037

21-
#[error("Operation cancelled by user.")]
38+
#[error("Operation cancelled by user")]
2239
Cancelled,
2340

24-
#[error("Potential secrets detected: {patterns:?}. Use --allow-secrets to proceed.")]
41+
#[error("Potential secrets detected: {patterns:?}")]
42+
#[diagnostic(
43+
code(commitbee::safety::secrets),
44+
help("Use --allow-secrets with local Ollama only")
45+
)]
2546
SecretsDetected { patterns: Vec<String> },
2647

27-
#[error("Cannot connect to Ollama at {host}. Is it running?")]
48+
#[error("Cannot connect to Ollama at {host}")]
49+
#[diagnostic(
50+
code(commitbee::ollama::not_running),
51+
help("Start Ollama with: ollama serve")
52+
)]
2853
OllamaNotRunning { host: String },
2954

3055
#[error("Model '{model}' not found. Available: {}", available.join(", "))]
56+
#[diagnostic(
57+
code(commitbee::ollama::model_not_found),
58+
help("Pull the model with: ollama pull {model}")
59+
)]
3160
ModelNotFound {
3261
model: String,
3362
available: Vec<String>,
3463
},
3564

3665
#[error("Provider '{provider}' error: {message}")]
66+
#[diagnostic(code(commitbee::provider::error))]
3767
Provider { provider: String, message: String },
3868

3969
#[error("Invalid commit message: {0}")]
70+
#[diagnostic(code(commitbee::commit::invalid))]
4071
InvalidCommitMessage(String),
4172

4273
#[error("Configuration error: {0}")]
74+
#[diagnostic(code(commitbee::config::error))]
4375
Config(String),
4476

4577
#[error("Git error: {0}")]
78+
#[diagnostic(code(commitbee::git::error))]
4679
Git(String),
4780

4881
#[error(transparent)]

src/main.rs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
// SPDX-License-Identifier: GPL-3.0-only
44

55
use clap::Parser;
6-
use console::style;
76

87
mod app;
98
mod cli;
@@ -17,24 +16,34 @@ use cli::Cli;
1716

1817
#[tokio::main]
1918
async fn main() {
19+
miette::set_hook(Box::new(|_| {
20+
Box::new(
21+
miette::MietteHandlerOpts::new()
22+
.terminal_links(true)
23+
.context_lines(2)
24+
.build(),
25+
)
26+
}))
27+
.ok();
28+
2029
let cli = Cli::parse();
2130

2231
let mut app = match App::new(cli) {
2332
Ok(app) => app,
2433
Err(e) => {
25-
eprintln!("{} {}", style("error:").red().bold(), e);
34+
eprintln!("{:?}", miette::Report::new(e));
2635
std::process::exit(1);
2736
}
2837
};
2938

3039
if let Err(e) = app.run().await {
3140
match e {
3241
error::Error::Cancelled => {
33-
eprintln!("{}", style("Aborted.").dim());
42+
eprintln!("Aborted.");
3443
std::process::exit(0);
3544
}
3645
_ => {
37-
eprintln!("{} {}", style("error:").red().bold(), e);
46+
eprintln!("{:?}", miette::Report::new(e));
3847
std::process::exit(1);
3948
}
4049
}

0 commit comments

Comments
 (0)