Improve raised exception consistency for media source (#66497)

This commit is contained in:
Paulus Schoutsen 2022-02-14 06:41:53 -08:00 committed by GitHub
parent 80394e3de6
commit 707f112f51
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 21 additions and 6 deletions

View file

@ -102,7 +102,10 @@ async def async_browse_media(
if DOMAIN not in hass.data:
raise BrowseError("Media Source not loaded")
item = await _get_media_item(hass, media_content_id).async_browse()
try:
item = await _get_media_item(hass, media_content_id).async_browse()
except ValueError as err:
raise BrowseError("Not a media source item") from err
if content_filter is None or item.children is None:
return item
@ -118,7 +121,13 @@ async def async_resolve_media(hass: HomeAssistant, media_content_id: str) -> Pla
"""Get info to play media."""
if DOMAIN not in hass.data:
raise Unresolvable("Media Source not loaded")
return await _get_media_item(hass, media_content_id).async_resolve()
try:
item = _get_media_item(hass, media_content_id)
except ValueError as err:
raise Unresolvable("Not a media source item") from err
return await item.async_resolve()
@websocket_api.websocket_command(

View file

@ -276,7 +276,7 @@ class UploadMediaView(HomeAssistantView):
uploaded_file: FileField = data["file"]
if not uploaded_file.content_type.startswith(("image/", "video/")):
if not uploaded_file.content_type.startswith(("image/", "video/", "audio/")):
LOGGER.error("Content type not allowed")
raise vol.Invalid("Only images and video are allowed")

View file

@ -60,7 +60,7 @@ async def test_async_browse_media(hass):
media.children[0].title = "Epic Sax Guy 10 Hours"
# Test invalid media content
with pytest.raises(ValueError):
with pytest.raises(BrowseError):
await media_source.async_browse_media(hass, "invalid")
# Test base URI returns all domains
@ -80,6 +80,8 @@ async def test_async_resolve_media(hass):
media_source.generate_media_source_id(media_source.DOMAIN, "local/test.mp3"),
)
assert isinstance(media, media_source.models.PlayMedia)
assert media.url == "/media/local/test.mp3"
assert media.mime_type == "audio/mpeg"
async def test_async_unresolve_media(hass):
@ -91,6 +93,10 @@ async def test_async_unresolve_media(hass):
with pytest.raises(media_source.Unresolvable):
await media_source.async_resolve_media(hass, "")
# Test invalid media content
with pytest.raises(media_source.Unresolvable):
await media_source.async_resolve_media(hass, "invalid")
async def test_websocket_browse_media(hass, hass_ws_client):
"""Test browse media websocket."""

View file

@ -52,9 +52,9 @@ async def test_async_browse_media(hass):
assert str(excinfo.value) == "Unknown source directory."
# Test invalid base
with pytest.raises(ValueError) as excinfo:
with pytest.raises(media_source.BrowseError) as excinfo:
await media_source.async_browse_media(hass, f"{const.URI_SCHEME}{DOMAIN}/")
assert str(excinfo.value) == "Invalid media source URI"
assert str(excinfo.value) == "Not a media source item"
# Test successful listing
media = await media_source.async_browse_media(