From 0b550bdaf4c7bc4bd176c5ee7aae112deb1cc211 Mon Sep 17 00:00:00 2001 From: Mike Crute Date: Mon, 12 Jun 2017 22:12:41 -0700 Subject: 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. --- pydora/player.py | 21 ++++++++++++++++++--- 1 file 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): Screen.print_error("No valid config found") sys.exit(1) - def station_selection_menu(self): + def station_selection_menu(self, error=None): """Format a station menu and make the user select a station """ Screen.clear() + if error: + Screen.print_error("{}\n".format(error)) + for i, station in enumerate(self.stations): i = "{:>3}".format(i) print(u"{}: {}".format(Colors.yellow(i), station.name)) @@ -205,7 +208,7 @@ class PlayerApp(object): try: cmd = getattr(self, self.CMD_MAP[input][1]) except (IndexError, KeyError): - return Screen.print_error("Invalid command!") + return Screen.print_error("Invalid command {!r}!".format(input)) cmd(song) @@ -222,10 +225,22 @@ class PlayerApp(object): self.client = self.get_client() self.stations = self.client.get_station_list() + error = None + while True: try: - station = self.station_selection_menu() + station = self.station_selection_menu(error) + error = None + except IndexError: + error = "Invalid station selection." + continue + except KeyboardInterrupt: + sys.exit(0) + + try: self.player.play_station(station) + except UnsupportedEncoding as ex: + error = str(ex) except KeyboardInterrupt: sys.exit(0) -- cgit v1.2.3