From 2df4de47a880a291dcd1376ef8ea10fb0e90a422 Mon Sep 17 00:00:00 2001 From: spl0k Date: Sat, 20 Jan 2018 22:46:41 +0100 Subject: [PATCH] Replaced most of catch-all exception handlers Closes #82 --- supysonic/api/__init__.py | 9 +++------ supysonic/api/albums_songs.py | 6 +++--- supysonic/api/annotation.py | 12 +++++------- supysonic/api/browse.py | 8 +++----- supysonic/api/chat.py | 2 +- supysonic/api/media.py | 6 +++--- supysonic/api/playlists.py | 2 +- supysonic/api/search.py | 6 +++--- supysonic/db.py | 2 +- supysonic/frontend/playlist.py | 8 ++++---- supysonic/managers/folder.py | 2 +- supysonic/managers/user.py | 2 +- supysonic/scanner.py | 2 +- supysonic/watcher.py | 2 +- 14 files changed, 31 insertions(+), 38 deletions(-) diff --git a/supysonic/api/__init__.py b/supysonic/api/__init__.py index 04e9b5c..d0104ad 100644 --- a/supysonic/api/__init__.py +++ b/supysonic/api/__init__.py @@ -231,16 +231,13 @@ def get_entity(req, cls, param = 'id'): try: eid = uuid.UUID(eid) - except: - return False, req.error_formatter(0, 'Invalid %s id' % cls.__name__) - - try: entity = cls[eid] + return True, entity + except ValueError: + return False, req.error_formatter(0, 'Invalid %s id' % cls.__name__) except ObjectNotFound: return False, (req.error_formatter(70, '%s not found' % cls.__name__), 404) - return True, entity - from .system import * from .browse import * from .user import * diff --git a/supysonic/api/albums_songs.py b/supysonic/api/albums_songs.py index 9ae4289..e54b5f6 100644 --- a/supysonic/api/albums_songs.py +++ b/supysonic/api/albums_songs.py @@ -39,7 +39,7 @@ def rand_songs(): fromYear = int(fromYear) if fromYear else None toYear = int(toYear) if toYear else None fid = uuid.UUID(musicFolderId) if musicFolderId else None - except: + except ValueError: return request.error_formatter(0, 'Invalid parameter format') query = Track.select() @@ -71,7 +71,7 @@ def album_list(): try: size = int(size) if size else 10 offset = int(offset) if offset else 0 - except: + except ValueError: return request.error_formatter(0, 'Invalid parameter format') query = select(t.folder for t in Track) @@ -114,7 +114,7 @@ def album_list_id3(): try: size = int(size) if size else 10 offset = int(offset) if offset else 0 - except: + except ValueError: return request.error_formatter(0, 'Invalid parameter format') query = Album.select() diff --git a/supysonic/api/annotation.py b/supysonic/api/annotation.py index 6f012c6..d6ebc9c 100644 --- a/supysonic/api/annotation.py +++ b/supysonic/api/annotation.py @@ -45,11 +45,9 @@ def try_star(cls, starred_cls, eid): try: uid = uuid.UUID(eid) - except: - return dict(code = 0, message = 'Invalid {} id {}'.format(cls.__name__, eid)) - - try: e = cls[uid] + except ValueError: + return dict(code = 0, message = 'Invalid {} id {}'.format(cls.__name__, eid)) except ObjectNotFound: return dict(code = 70, message = 'Unknown {} id {}'.format(cls.__name__, eid)) @@ -73,7 +71,7 @@ def try_unstar(starred_cls, eid): try: uid = uuid.UUID(eid) - except: + except ValueError: return dict(code = 0, message = 'Invalid id {}'.format(eid)) delete(s for s in starred_cls if s.user.id == request.user.id and s.starred.id == uid) @@ -145,7 +143,7 @@ def rate(): try: uid = uuid.UUID(id) rating = int(rating) - except: + except ValueError: return request.error_formatter(0, 'Invalid parameter') if not 0 <= rating <= 5: @@ -186,7 +184,7 @@ def scrobble(): if t: try: t = int(t) / 1000 - except: + except ValueError: return request.error_formatter(0, 'Invalid time value') else: t = int(time.time()) diff --git a/supysonic/api/browse.py b/supysonic/api/browse.py index adddc71..150ef43 100644 --- a/supysonic/api/browse.py +++ b/supysonic/api/browse.py @@ -50,7 +50,7 @@ def list_indexes(): if ifModifiedSince: try: ifModifiedSince = int(ifModifiedSince) / 1000 - except: + except ValueError: return request.error_formatter(0, 'Invalid timestamp') if musicFolderId is None: @@ -58,11 +58,9 @@ def list_indexes(): else: try: mfid = uuid.UUID(musicFolderId) - except: - return request.error_formatter(0, 'Invalid id') - - try: folder = Folder[mfid] + except ValueError: + return request.error_formatter(0, 'Invalid id') except ObjectNotFound: return request.error_formatter(70, 'Folder not found') if not folder.root: diff --git a/supysonic/api/chat.py b/supysonic/api/chat.py index 0fa4920..8e1421a 100644 --- a/supysonic/api/chat.py +++ b/supysonic/api/chat.py @@ -29,7 +29,7 @@ def get_chat(): since = request.values.get('since') try: since = int(since) / 1000 if since else None - except: + except ValueError: return request.error_formatter(0, 'Invalid parameter') with db_session: diff --git a/supysonic/api/media.py b/supysonic/api/media.py index 6af08e8..3203c53 100644 --- a/supysonic/api/media.py +++ b/supysonic/api/media.py @@ -70,7 +70,7 @@ def stream_media(): if maxBitRate: try: maxBitRate = int(maxBitRate) - except: + except ValueError: return request.error_formatter(0, 'Invalid bitrate value') if dst_bitrate > maxBitRate and maxBitRate != 0: @@ -100,7 +100,7 @@ def stream_media(): else: dec_proc = subprocess.Popen(decoder, stdout = subprocess.PIPE) proc = subprocess.Popen(encoder, stdin = dec_proc.stdout, stdout = subprocess.PIPE) - except: + except OSError: return request.error_formatter(0, 'Error while running the transcoding process') def transcode(): @@ -155,7 +155,7 @@ def cover_art(): if size: try: size = int(size) - except: + except ValueError: return request.error_formatter(0, 'Invalid size value') else: return send_file(os.path.join(res.path, 'cover.jpg')) diff --git a/supysonic/api/playlists.py b/supysonic/api/playlists.py index 17e997f..4d2fee6 100644 --- a/supysonic/api/playlists.py +++ b/supysonic/api/playlists.py @@ -70,7 +70,7 @@ def create_playlist(): songs = request.values.getlist('songId') try: playlist_id = uuid.UUID(playlist_id) if playlist_id else None - except: + except ValueError: return request.error_formatter(0, 'Invalid playlist id') if playlist_id: diff --git a/supysonic/api/search.py b/supysonic/api/search.py index 847362a..9366a85 100644 --- a/supysonic/api/search.py +++ b/supysonic/api/search.py @@ -33,7 +33,7 @@ def old_search(): count = int(count) if count else 20 offset = int(offset) if offset else 0 newer_than = int(newer_than) / 1000 if newer_than else 0 - except: + except ValueError: return request.error_formatter(0, 'Invalid parameter') min_date = datetime.fromtimestamp(newer_than) @@ -82,7 +82,7 @@ def new_search(): album_offset = int(album_offset) if album_offset else 0 song_count = int(song_count) if song_count else 20 song_offset = int(song_offset) if song_offset else 0 - except: + except ValueError: return request.error_formatter(0, 'Invalid parameter') if not query: @@ -111,7 +111,7 @@ def search_id3(): album_offset = int(album_offset) if album_offset else 0 song_count = int(song_count) if song_count else 20 song_offset = int(song_offset) if song_offset else 0 - except: + except ValueError: return request.error_formatter(0, 'Invalid parameter') if not query: diff --git a/supysonic/db.py b/supysonic/db.py index 019524c..2ceebf2 100644 --- a/supysonic/db.py +++ b/supysonic/db.py @@ -404,7 +404,7 @@ class Playlist(db.Entity): tid = UUID(t) track = Track[tid] tracks.append(track) - except: + except (ValueError, ObjectNotFound): should_fix = True if should_fix: diff --git a/supysonic/frontend/playlist.py b/supysonic/frontend/playlist.py index d3f62d2..1e7d8ad 100644 --- a/supysonic/frontend/playlist.py +++ b/supysonic/frontend/playlist.py @@ -39,7 +39,7 @@ def playlist_index(): def playlist_details(uid): try: uid = uuid.UUID(uid) - except: + except ValueError: flash('Invalid playlist id') return redirect(url_for('playlist_index')) @@ -56,7 +56,7 @@ def playlist_details(uid): def playlist_update(uid): try: uid = uuid.UUID(uid) - except: + except ValueError: flash('Invalid playlist id') return redirect(url_for('playlist_index')) @@ -75,14 +75,14 @@ def playlist_update(uid): playlist.public = request.form.get('public') in (True, 'True', 1, '1', 'on', 'checked') flash('Playlist updated.') - return playlist_details(uid) + return playlist_details(str(uid)) @app.route('/playlist/del/') @db_session def playlist_delete(uid): try: uid = uuid.UUID(uid) - except: + except ValueError: flash('Invalid playlist id') return redirect(url_for('playlist_index')) diff --git a/supysonic/managers/folder.py b/supysonic/managers/folder.py index d4027ba..8cd89bb 100644 --- a/supysonic/managers/folder.py +++ b/supysonic/managers/folder.py @@ -43,7 +43,7 @@ class FolderManager: if isinstance(uid, strtype): try: uid = uuid.UUID(uid) - except: + except ValueError: return FolderManager.INVALID_ID, None elif isinstance(uid, uuid.UUID): pass diff --git a/supysonic/managers/user.py b/supysonic/managers/user.py index 1eac546..2931eef 100644 --- a/supysonic/managers/user.py +++ b/supysonic/managers/user.py @@ -35,7 +35,7 @@ class UserManager: if isinstance(uid, strtype): try: uid = uuid.UUID(uid) - except: + except ValueError: return UserManager.INVALID_ID, None elif isinstance(uid, uuid.UUID): pass diff --git a/supysonic/scanner.py b/supysonic/scanner.py index e3cef28..f6dfcfd 100644 --- a/supysonic/scanner.py +++ b/supysonic/scanner.py @@ -310,7 +310,7 @@ class Scanner: if transform: value = transform(value) return value if value else default - except: + except KeyError: return default def stats(self): diff --git a/supysonic/watcher.py b/supysonic/watcher.py index a772261..a605d4b 100644 --- a/supysonic/watcher.py +++ b/supysonic/watcher.py @@ -246,7 +246,7 @@ class SupysonicWatcher(object): try: signal(SIGTERM, self.__terminate) signal(SIGINT, self.__terminate) - except: + except ValueError: logger.warning('Unable to set signal handlers') queue.start()