Skip to content

Commit 80f482d

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

File tree

3 files changed

+19
-12
lines changed

3 files changed

+19
-12
lines changed

README.md

+6-6
Original file line numberDiff line numberDiff line change
@@ -67,16 +67,16 @@ bundle = rustfluent.Bundle(
6767

6868
#### Parameters
6969

70-
| Name | Type | Description |
71-
|-------------|------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------|
72-
| `language` | `str` | [Unicode Language Identifier](https://unicode.org/reports/tr35/tr35.html#Unicode_language_identifier) for the language. |
73-
| `ftl_files` | `list[str]` | Full paths to the FTL files containing the translations. Entries in later files overwrite earlier ones. |
74-
| `strict` | `bool`, optional | In strict mode, a `ValueError` will be raised if there are any errors in the file. In non-strict mode, invalid Fluent messages will be excluded from the Bundle. |
70+
| Name | Type | Description |
71+
|-------------|------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
72+
| `language` | `str` | [Unicode Language Identifier](https://unicode.org/reports/tr35/tr35.html#Unicode_language_identifier) for the language. |
73+
| `ftl_files` | `list[str]` | Full paths to the FTL files containing the translations. Entries in later files overwrite earlier ones. |
74+
| `strict` | `bool`, optional | In strict mode, a `ParserError` will be raised if there are any errors in the file. In non-strict mode, invalid Fluent messages will be excluded from the Bundle. |
7575

7676
#### Raises
7777

7878
- `FileNotFoundError` if any of the FTL files could not be found.
79-
- `ValueError` if any of the FTL files contain errors (strict mode only).
79+
- `rustfluent.ParserError` if any of the FTL files contain errors (strict mode only).
8080

8181
### `Bundle.get_translation`
8282

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)