Skip to content

Commit

Permalink
feat: Better error on missing icons (#9)
Browse files Browse the repository at this point in the history
* chore: add link to error message

* fix test

* add coverage
  • Loading branch information
tlambert03 authored Oct 10, 2023
1 parent 6f456bd commit 34daa4c
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 18 deletions.
49 changes: 32 additions & 17 deletions src/pyconify/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import atexit
import os
import re
import tempfile
import warnings
from contextlib import suppress
Expand Down Expand Up @@ -86,7 +87,11 @@ def collection(
resp = requests.get(f"{ROOT}/collection?prefix={prefix}", params=query_params)
resp.raise_for_status()
if (content := resp.json()) == 404:
raise requests.HTTPError(f"Icon set {prefix!r} not found.", response=resp)
raise requests.HTTPError(
f"Icon set {prefix!r} not found. "
"Search for icons at https://icon-sets.iconify.design",
response=resp,
)
return content # type: ignore


Expand Down Expand Up @@ -193,7 +198,11 @@ def svg(
resp = requests.get(f"{ROOT}/{prefix}/{name}.svg", params=query_params)
resp.raise_for_status()
if resp.content == b"404":
raise requests.HTTPError(f"Icon '{prefix}:{name}' not found.", response=resp)
raise requests.HTTPError(
f"Icon '{prefix}:{name}' not found. "
f"Search for icons at https://icon-sets.iconify.design?query={name}",
response=resp,
)

# cache response and return
cache[svg_cache_key] = resp.content
Expand Down Expand Up @@ -354,27 +363,29 @@ def css(
"""
prefix, icons = _split_prefix_name(keys, allow_many=True)
params: dict = {}
if selector is not None:
params["selector"] = selector
if common is not None:
params["common"] = common
if override is not None:
params["override"] = override

for k in ("selector", "common", "override", "var", "color", "mode", "format"):
if (val := locals()[k]) is not None:
params[k] = val
if pseudo:
params["pseudo"] = 1
if var is not None:
params["var"] = var
if square:
params["square"] = 1
if color is not None:
params["color"] = color
if mode is not None:
params["mode"] = mode
if format is not None:
params["format"] = format

resp = requests.get(f"{ROOT}/{prefix}.css?icons={','.join(icons)}", params=params)
resp.raise_for_status()
if resp.text == "404":
raise requests.HTTPError(
f"Icon set {prefix!r} not found. "
"Search for icons at https://icon-sets.iconify.design",
response=resp,
)
if missing := set(re.findall(r"Could not find icon: ([^\s]*) ", resp.text)):
warnings.warn(
f"Icon(s) {sorted(missing)} not found. "
"Search for icons at https://icon-sets.iconify.design",
stacklevel=2,
)
return resp.text


Expand All @@ -400,7 +411,11 @@ def icon_data(*keys: str) -> IconifyJSON:
resp = requests.get(f"{ROOT}/{prefix}.json?icons={','.join(names)}")
resp.raise_for_status()
if (content := resp.json()) == 404:
raise requests.HTTPError(f"No data returned for {prefix!r}", response=resp)
raise requests.HTTPError(
f"Icon set {prefix!r} not found. "
"Search for icons at https://icon-sets.iconify.design",
response=resp,
)
return content # type: ignore


Expand Down
8 changes: 7 additions & 1 deletion tests/test_pyconify.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def test_icon_data() -> None:
assert result["prefix"] == "bi"
assert "alarm" in result["icons"]

with pytest.raises(IOError, match="No data returned"):
with pytest.raises(IOError, match="Icon set 'not' not found"):
pyconify.icon_data("not", "found")


Expand Down Expand Up @@ -92,6 +92,12 @@ def test_css() -> None:
)
assert result2.startswith("common")

with pytest.raises(IOError, match="Icon set 'not' not found."):
pyconify.css("not:found")

with pytest.warns(UserWarning, match=r"Icon\(s\) \['nor-this', 'not-an-icon'\]"):
pyconify.css("bi:not-an-icon,nor-this")


def test_last_modified() -> None:
assert isinstance(pyconify.last_modified("bi")["bi"], int)
Expand Down

0 comments on commit 34daa4c

Please sign in to comment.