aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMike Crute <mcrute@gmail.com>2017-06-12 22:12:41 -0700
committerMike Crute <mcrute@gmail.com>2017-06-12 22:22:38 -0700
commit0b550bdaf4c7bc4bd176c5ee7aae112deb1cc211 (patch)
tree0bc65c6317a6ceb36afdc01ff186bd739973e110
parentd2a2161fd2dace81abef246003d3884af579f789 (diff)
downloadpydora-0b550bdaf4c7bc4bd176c5ee7aae112deb1cc211.tar.bz2
pydora-0b550bdaf4c7bc4bd176c5ee7aae112deb1cc211.tar.xz
pydora-0b550bdaf4c7bc4bd176c5ee7aae112deb1cc211.zip
Better error handling
Handles the case where a user chooses a non-existent station and sends them back to the selection menu with a reasonable error message. Also prints the typed characters for invalid input during playback and prints errors from the audio backend such as unsupported formats.
-rwxr-xr-xpydora/player.py21
1 files changed, 18 insertions, 3 deletions
diff --git a/pydora/player.py b/pydora/player.py
index b1685dc..2c93fb5 100755
--- a/pydora/player.py
+++ b/pydora/player.py
@@ -97,11 +97,14 @@ class PlayerApp(object):
97 Screen.print_error("No valid config found") 97 Screen.print_error("No valid config found")
98 sys.exit(1) 98 sys.exit(1)
99 99
100 def station_selection_menu(self): 100 def station_selection_menu(self, error=None):
101 """Format a station menu and make the user select a station 101 """Format a station menu and make the user select a station
102 """ 102 """
103 Screen.clear() 103 Screen.clear()
104 104
105 if error:
106 Screen.print_error("{}\n".format(error))
107
105 for i, station in enumerate(self.stations): 108 for i, station in enumerate(self.stations):
106 i = "{:>3}".format(i) 109 i = "{:>3}".format(i)
107 print(u"{}: {}".format(Colors.yellow(i), station.name)) 110 print(u"{}: {}".format(Colors.yellow(i), station.name))
@@ -205,7 +208,7 @@ class PlayerApp(object):
205 try: 208 try:
206 cmd = getattr(self, self.CMD_MAP[input][1]) 209 cmd = getattr(self, self.CMD_MAP[input][1])
207 except (IndexError, KeyError): 210 except (IndexError, KeyError):
208 return Screen.print_error("Invalid command!") 211 return Screen.print_error("Invalid command {!r}!".format(input))
209 212
210 cmd(song) 213 cmd(song)
211 214
@@ -222,10 +225,22 @@ class PlayerApp(object):
222 self.client = self.get_client() 225 self.client = self.get_client()
223 self.stations = self.client.get_station_list() 226 self.stations = self.client.get_station_list()
224 227
228 error = None
229
225 while True: 230 while True:
226 try: 231 try:
227 station = self.station_selection_menu() 232 station = self.station_selection_menu(error)
233 error = None
234 except IndexError:
235 error = "Invalid station selection."
236 continue
237 except KeyboardInterrupt:
238 sys.exit(0)
239
240 try:
228 self.player.play_station(station) 241 self.player.play_station(station)
242 except UnsupportedEncoding as ex:
243 error = str(ex)
229 except KeyboardInterrupt: 244 except KeyboardInterrupt:
230 sys.exit(0) 245 sys.exit(0)
231 246