Skip to content

Commit

Permalink
Improving error handling (#678)
Browse files Browse the repository at this point in the history
* added macro for throwing errors
 changed parse code to inline parse_declaration

* Fixed capitialisation

* changed errors in parse_model to macro

* changed --version to output the working
githash instead of conjure version
also added git hash to bug!() macro

* fixing odd macro bug

* added location to bug

* made input file optional.

* removed unused import

* removed empty line between comment and macro
  • Loading branch information
TAswan authored Feb 14, 2025
1 parent 5d613db commit e9ec0eb
Show file tree
Hide file tree
Showing 7 changed files with 129 additions and 83 deletions.
22 changes: 22 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions conjure_oxide/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ tracing = "0.1.41"
tree-sitter = "0.24.7"
tree-sitter-essence = { version = "0.1.0", path = "../crates/tree-sitter-essence" }
tree-sitter-haskell = "0.23.0"
git-version = "0.3.9"

[features]

Expand Down
29 changes: 21 additions & 8 deletions conjure_oxide/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,19 @@ use conjure_oxide::{get_rules, model_from_json};

use conjure_oxide::utils::conjure::{get_minion_solutions, minion_solutions_to_json};
use conjure_oxide::SolverFamily;
use git_version::git_version;
use tracing_subscriber::filter::LevelFilter;
use tracing_subscriber::layer::SubscriberExt as _;
use tracing_subscriber::util::SubscriberInitExt as _;
use tracing_subscriber::{EnvFilter, Layer};

static AFTER_HELP_TEXT: &str = include_str!("help_text.txt");

#[derive(Parser)]
#[command(author, version, about, long_about = None, after_long_help=AFTER_HELP_TEXT)]
#[derive(Parser, Clone)]
#[command(author, about, long_about = None, after_long_help=AFTER_HELP_TEXT)]
struct Cli {
#[arg(value_name = "INPUT_ESSENCE", help = "The input Essence file")]
input_file: PathBuf,
input_file: Option<PathBuf>,

#[arg(
long,
Expand Down Expand Up @@ -64,6 +65,13 @@ struct Cli {
)]
print_info_schema: bool,

#[arg(
long = "version",
short = 'V',
help = "Print the version of the program (git commit) and exit"
)]
version: bool,

#[arg(long, help = "Save execution info as JSON to the given file-path.")]
info_json_path: Option<PathBuf>,

Expand Down Expand Up @@ -183,6 +191,11 @@ pub fn main() -> AnyhowResult<()> {
)
};

if cli.version {
println!("Version: {}", git_version!());
return Ok(());
}

// load the loggers
tracing_subscriber::registry()
.with(json_layer)
Expand Down Expand Up @@ -222,9 +235,9 @@ pub fn main() -> AnyhowResult<()> {
"Rules: {}",
rules.iter().map(|rd| format!("{}", rd)).collect::<Vec<_>>().join("\n")
);

tracing::info!(target: "file", "Input file: {}", cli.input_file.display());
let input_file: &str = cli.input_file.to_str().ok_or(anyhow!(
let input = cli.input_file.clone().expect("No input file given");
tracing::info!(target: "file", "Input file: {}", input.display());
let input_file: &str = input.to_str().ok_or(anyhow!(
"Given input_file could not be converted to a string"
))?;

Expand Down Expand Up @@ -256,7 +269,7 @@ pub fn main() -> AnyhowResult<()> {
rule_sets.clone(),
);

context.write().unwrap().file_name = Some(cli.input_file.to_str().expect("").into());
context.write().unwrap().file_name = Some(input.to_str().expect("").into());

if cfg!(feature = "extra-rule-checks") {
tracing::info!("extra-rule-checks: enabled");
Expand All @@ -283,7 +296,7 @@ pub fn main() -> AnyhowResult<()> {
if cli.no_run_solver {
println!("{}", model);
} else {
run_solver(&cli, model)?;
run_solver(&cli.clone(), model)?;
}

// still do postamble even if we didn't run the solver
Expand Down
1 change: 1 addition & 0 deletions crates/conjure_core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ schemars = "0.8.21"
clap = { version = "4.5.28", features = ["derive"] }
itertools = "0.14.0"
im = "15.1.0"
git-version = "0.3.9"

[lints]
workspace = true
5 changes: 4 additions & 1 deletion crates/conjure_core/src/bug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,11 @@ You can help us by providing a minimal failing example.
Issue tracker: http://github.com/conjure-cp/conjure-oxide/issues
version: {}
location: {}:{}:{}
{}
"#, &formatted_msg);
"#, git_version::git_version!(),file!(),module_path!(),line!(), &formatted_msg);

panic!("{}", full_message);
}};
Expand Down
30 changes: 30 additions & 0 deletions crates/conjure_core/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,33 @@ pub enum Error {
#[error(transparent)]
Other(#[from] anyhow::Error),
}

// Macro to throw an error with the line number and function name
#[macro_export]
macro_rules! throw_error {
($msg:expr) => {{
let error_msg = format!(
" {} | File: {} | Function: {} | Line: {}",
$msg,
file!(),
module_path!(),
line!()
);
Err(Error::Parse(error_msg))
}};
}

// Macro to add an error with the line number and function name (for functions that take an error like ok_or)
#[macro_export]
macro_rules! error {
($msg:expr) => {{
let error_msg = format!(
" {} | File: {} | Function: {} | Line: {}",
$msg,
file!(),
module_path!(),
line!()
);
Error::Parse(error_msg)
}};
}
Loading

0 comments on commit e9ec0eb

Please sign in to comment.