Skip to content

Commit

Permalink
provide a more helpful error message if parsing the font family param…
Browse files Browse the repository at this point in the history
…eter failed
  • Loading branch information
FriedrichFroebel committed Sep 20, 2024
1 parent 6215832 commit 166f0da
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 5 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# ignore config.json
# Ignore custom configuration file.
config.json

# Ignore test files.
sample-out.png

### Python template
# Byte-compiled / optimized / DLL files
__pycache__/
Expand Down
11 changes: 8 additions & 3 deletions brother_ql_web/web.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,12 @@ def get_label_parameters(
font_family = ""
font_style = ""
else:
raise
raise ValueError(
"Could not find valid font specifier. Please pass the `font_family` "
"parameter with the family and style in the format `Roboto (Medium)`, "
"where Roboto is the family name and Medium the corresponding font "
"style."
)
context = {
"text": d.get("text", ""),
"image": _save_to_bytes(request.files.get("image")),
Expand Down Expand Up @@ -141,7 +146,7 @@ def print_text() -> dict[str, bool | str]:

try:
parameters = get_label_parameters(bottle.request)
except (AttributeError, LookupError) as e:
except (AttributeError, LookupError, ValueError) as e:
return_dict["error"] = str(e)
return return_dict

Expand Down Expand Up @@ -169,7 +174,7 @@ def print_image() -> dict[str, bool | str]:

try:
parameters = get_label_parameters(bottle.request, should_be_file=True)
except (AttributeError, LookupError) as e:
except (AttributeError, LookupError, ValueError) as e:
return_dict["error"] = str(e)
return return_dict

Expand Down
5 changes: 4 additions & 1 deletion tests/test_web.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,10 @@ def test_error(self) -> None:
self.assertEqual(
(
b'{"success": false, '
b"\"error\": \"'NoneType' object has no attribute 'rpartition'\"}"
b'"error": "Could not find valid font specifier. '
b'Please pass the `font_family` parameter with the family and style '
b'in the format `Roboto (Medium)`, where Roboto is the family name '
b'and Medium the corresponding font style."}'
),
response.content,
)
Expand Down

0 comments on commit 166f0da

Please sign in to comment.