Skip to content

Commit

Permalink
Report CLI error in main function (#13)
Browse files Browse the repository at this point in the history
* refactor: use proper PyValueError constructor

* feat: report CLI error

`run_cli` was just swallowing the error, so nothing was displayed to the
user if an error happened.
  • Loading branch information
oyarsa authored Jan 15, 2025
1 parent c2f2b6c commit 710e16f
Showing 1 changed file with 5 additions and 9 deletions.
14 changes: 5 additions & 9 deletions src/python.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,12 @@ fn extract_annotations(content: &str, file_type: &str) -> PyResult<Vec<PyAnnotat
"py" => FileType::Python,
"rs" => FileType::Rust,
"js" => FileType::JavaScript,
_ => return Err(PyErr::new::<PyValueError, _>("Invalid file type")),
_ => return Err(PyValueError::new_err("Invalid file type")),
};

let dummy_path = std::path::PathBuf::from("<string>");
let annotations = parser::extract_annotations(content, &ft, &dummy_path)
.map_err(|e| PyErr::new::<PyValueError, _>(e.to_string()))?;
.map_err(|e| PyValueError::new_err(e.to_string()))?;

Ok(annotations
.into_iter()
Expand Down Expand Up @@ -118,22 +118,18 @@ fn format_annotations(annotations: Vec<PyAnnotation>, format: &str) -> PyResult<
let adapter = match format {
"json" => RenderAdapter::Json(JsonAdapter),
"yaml" => RenderAdapter::Yaml(YamlAdapter),
_ => {
return Err(PyErr::new::<pyo3::exceptions::PyValueError, _>(
"Invalid format",
))
}
_ => return Err(PyValueError::new_err("Invalid format")),
};

adapter
.format(&annotations)
.map_err(|e| PyErr::new::<pyo3::exceptions::PyValueError, _>(e.to_string()))
.map_err(|e| PyValueError::new_err(e.to_string()))
}

// @todo: remove once https://github.com/PyO3/maturin/issues/368 is resolved
#[pyfunction]
fn run_cli(args: Vec<String>) -> PyResult<()> {
let _ = run(args);
run(args).map_err(|err| PyValueError::new_err(err.to_string()))?;
Ok(())
}

Expand Down

0 comments on commit 710e16f

Please sign in to comment.