Skip to content

Commit 6e3557e

Browse files
committed
Better error handling
1 parent 2e1a6b1 commit 6e3557e

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

custom_components/svt_play/video_url_fetch/live_test.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from video_fetch import video_url_by_channel, video_id_by_time, video_url_by_video_id, suggested_video_id
2+
import pytest
23

34

45
def test_rapport_by_time():
@@ -16,3 +17,18 @@ def test_rapport_by_suggested():
1617
def test_svt1():
1718
url = video_url_by_channel("svt1")
1819
assert url.startswith('http')
20+
21+
22+
def test_not_found_by_time():
23+
with pytest.raises(Exception, match="Could not find program with id: bogus"):
24+
video_id_by_time('bogus')
25+
26+
27+
def test_not_found_by_suggested():
28+
with pytest.raises(Exception, match="Could not find program with id: bogus"):
29+
suggested_video_id('bogus')
30+
31+
32+
def test_not_found_channel():
33+
with pytest.raises(Exception, match="Could not fetch video url: Not found"):
34+
video_url_by_channel("svt1337")

custom_components/svt_play/video_url_fetch/video_fetch.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,10 @@ def video_url_by_channel(channel_id, formats=default_formats):
2121

2222
def video_url_from_videoplayer_api(url, formats):
2323
data = get(url).json()
24-
if 'msg' in data:
25-
raise Exception("Could not fetch the CDN data: {}".format(data['msg']))
24+
if 'message' in data:
25+
raise Exception(
26+
"Could not fetch video url: {}".format(data['message'])
27+
)
2628

2729
for format in formats:
2830
for video_reference in data['videoReferences']:
@@ -107,6 +109,11 @@ def information_by_program_id(program_id):
107109
}
108110

109111
data = post('https://api.svt.se/contento/graphql', json=query_data).json()
112+
if len(data['data']['listablesBySlug']) < 1:
113+
raise Exception(
114+
"Could not find program with id: {}".format(program_id)
115+
)
116+
110117
associated_content = data['data']['listablesBySlug'][0]
111118

112119
return associated_content

0 commit comments

Comments
 (0)