Skip to content

Commit bd7a8ec

Browse files
committed
Define ParserError exception
To give client code more control about what exceptions it catches.
1 parent a5c1309 commit bd7a8ec

File tree

2 files changed

+13
-6
lines changed

2 files changed

+13
-6
lines changed

src/lib.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,14 @@ use pyo3::types::{PyDict, PyList};
77
use std::fs;
88
use unic_langid::LanguageIdentifier;
99

10+
use pyo3::create_exception;
11+
12+
create_exception!(rustfluent, PyParserError, pyo3::exceptions::PyException);
13+
1014
#[pymodule]
1115
fn rustfluent(m: &Bound<'_, PyModule>) -> PyResult<()> {
1216
m.add_class::<Bundle>()?;
17+
m.add("ParserError", m.py().get_type_bound::<PyParserError>())?;
1318
Ok(())
1419
}
1520

@@ -37,8 +42,9 @@ impl Bundle {
3742
Ok(resource) => resource,
3843
Err(error) => {
3944
if strict {
40-
return Err(PyValueError::new_err(format!(
41-
"{error:?} - Fluent file contains errors"
45+
return Err(PyParserError::new_err(format!(
46+
"Error when parsing {}.",
47+
file_path
4248
)));
4349
} else {
4450
// The first element of the error is the parsed resource, minus any

tests/test_python_interface.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env python
2-
32
import pathlib
3+
import re
44

55
import pytest
66

@@ -78,6 +78,7 @@ def test_parses_other_parts_of_file_that_contains_errors_in_non_strict_mode(
7878
assert translation == "I'm valid."
7979

8080

81-
def test_raises_value_error_on_file_that_contains_errors_in_strict_mode():
82-
with pytest.raises(ValueError):
83-
fluent.Bundle("fr", [str(data_dir / "errors.ftl")], strict=True)
81+
def test_raises_parser_error_on_file_that_contains_errors_in_strict_mode():
82+
filename = str(data_dir / "errors.ftl")
83+
with pytest.raises(fluent.ParserError, match=re.escape(f"Error when parsing {filename}.")):
84+
fluent.Bundle("fr", [filename], strict=True)

0 commit comments

Comments
 (0)