1
0
mirror of https://github.com/spl0k/supysonic.git synced 2024-11-10 04:02:17 +00:00

Replaced most of catch-all exception handlers

Closes #82
This commit is contained in:
spl0k 2018-01-20 22:46:41 +01:00
parent 3fd748cc5a
commit 2df4de47a8
14 changed files with 31 additions and 38 deletions

View File

@ -231,16 +231,13 @@ def get_entity(req, cls, param = 'id'):
try: try:
eid = uuid.UUID(eid) eid = uuid.UUID(eid)
except:
return False, req.error_formatter(0, 'Invalid %s id' % cls.__name__)
try:
entity = cls[eid] entity = cls[eid]
return True, entity
except ValueError:
return False, req.error_formatter(0, 'Invalid %s id' % cls.__name__)
except ObjectNotFound: except ObjectNotFound:
return False, (req.error_formatter(70, '%s not found' % cls.__name__), 404) return False, (req.error_formatter(70, '%s not found' % cls.__name__), 404)
return True, entity
from .system import * from .system import *
from .browse import * from .browse import *
from .user import * from .user import *

View File

@ -39,7 +39,7 @@ def rand_songs():
fromYear = int(fromYear) if fromYear else None fromYear = int(fromYear) if fromYear else None
toYear = int(toYear) if toYear else None toYear = int(toYear) if toYear else None
fid = uuid.UUID(musicFolderId) if musicFolderId else None fid = uuid.UUID(musicFolderId) if musicFolderId else None
except: except ValueError:
return request.error_formatter(0, 'Invalid parameter format') return request.error_formatter(0, 'Invalid parameter format')
query = Track.select() query = Track.select()
@ -71,7 +71,7 @@ def album_list():
try: try:
size = int(size) if size else 10 size = int(size) if size else 10
offset = int(offset) if offset else 0 offset = int(offset) if offset else 0
except: except ValueError:
return request.error_formatter(0, 'Invalid parameter format') return request.error_formatter(0, 'Invalid parameter format')
query = select(t.folder for t in Track) query = select(t.folder for t in Track)
@ -114,7 +114,7 @@ def album_list_id3():
try: try:
size = int(size) if size else 10 size = int(size) if size else 10
offset = int(offset) if offset else 0 offset = int(offset) if offset else 0
except: except ValueError:
return request.error_formatter(0, 'Invalid parameter format') return request.error_formatter(0, 'Invalid parameter format')
query = Album.select() query = Album.select()

View File

@ -45,11 +45,9 @@ def try_star(cls, starred_cls, eid):
try: try:
uid = uuid.UUID(eid) uid = uuid.UUID(eid)
except:
return dict(code = 0, message = 'Invalid {} id {}'.format(cls.__name__, eid))
try:
e = cls[uid] e = cls[uid]
except ValueError:
return dict(code = 0, message = 'Invalid {} id {}'.format(cls.__name__, eid))
except ObjectNotFound: except ObjectNotFound:
return dict(code = 70, message = 'Unknown {} id {}'.format(cls.__name__, eid)) return dict(code = 70, message = 'Unknown {} id {}'.format(cls.__name__, eid))
@ -73,7 +71,7 @@ def try_unstar(starred_cls, eid):
try: try:
uid = uuid.UUID(eid) uid = uuid.UUID(eid)
except: except ValueError:
return dict(code = 0, message = 'Invalid id {}'.format(eid)) 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) 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: try:
uid = uuid.UUID(id) uid = uuid.UUID(id)
rating = int(rating) rating = int(rating)
except: except ValueError:
return request.error_formatter(0, 'Invalid parameter') return request.error_formatter(0, 'Invalid parameter')
if not 0 <= rating <= 5: if not 0 <= rating <= 5:
@ -186,7 +184,7 @@ def scrobble():
if t: if t:
try: try:
t = int(t) / 1000 t = int(t) / 1000
except: except ValueError:
return request.error_formatter(0, 'Invalid time value') return request.error_formatter(0, 'Invalid time value')
else: else:
t = int(time.time()) t = int(time.time())

View File

@ -50,7 +50,7 @@ def list_indexes():
if ifModifiedSince: if ifModifiedSince:
try: try:
ifModifiedSince = int(ifModifiedSince) / 1000 ifModifiedSince = int(ifModifiedSince) / 1000
except: except ValueError:
return request.error_formatter(0, 'Invalid timestamp') return request.error_formatter(0, 'Invalid timestamp')
if musicFolderId is None: if musicFolderId is None:
@ -58,11 +58,9 @@ def list_indexes():
else: else:
try: try:
mfid = uuid.UUID(musicFolderId) mfid = uuid.UUID(musicFolderId)
except:
return request.error_formatter(0, 'Invalid id')
try:
folder = Folder[mfid] folder = Folder[mfid]
except ValueError:
return request.error_formatter(0, 'Invalid id')
except ObjectNotFound: except ObjectNotFound:
return request.error_formatter(70, 'Folder not found') return request.error_formatter(70, 'Folder not found')
if not folder.root: if not folder.root:

View File

@ -29,7 +29,7 @@ def get_chat():
since = request.values.get('since') since = request.values.get('since')
try: try:
since = int(since) / 1000 if since else None since = int(since) / 1000 if since else None
except: except ValueError:
return request.error_formatter(0, 'Invalid parameter') return request.error_formatter(0, 'Invalid parameter')
with db_session: with db_session:

View File

@ -70,7 +70,7 @@ def stream_media():
if maxBitRate: if maxBitRate:
try: try:
maxBitRate = int(maxBitRate) maxBitRate = int(maxBitRate)
except: except ValueError:
return request.error_formatter(0, 'Invalid bitrate value') return request.error_formatter(0, 'Invalid bitrate value')
if dst_bitrate > maxBitRate and maxBitRate != 0: if dst_bitrate > maxBitRate and maxBitRate != 0:
@ -100,7 +100,7 @@ def stream_media():
else: else:
dec_proc = subprocess.Popen(decoder, stdout = subprocess.PIPE) dec_proc = subprocess.Popen(decoder, stdout = subprocess.PIPE)
proc = subprocess.Popen(encoder, stdin = dec_proc.stdout, 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') return request.error_formatter(0, 'Error while running the transcoding process')
def transcode(): def transcode():
@ -155,7 +155,7 @@ def cover_art():
if size: if size:
try: try:
size = int(size) size = int(size)
except: except ValueError:
return request.error_formatter(0, 'Invalid size value') return request.error_formatter(0, 'Invalid size value')
else: else:
return send_file(os.path.join(res.path, 'cover.jpg')) return send_file(os.path.join(res.path, 'cover.jpg'))

View File

@ -70,7 +70,7 @@ def create_playlist():
songs = request.values.getlist('songId') songs = request.values.getlist('songId')
try: try:
playlist_id = uuid.UUID(playlist_id) if playlist_id else None playlist_id = uuid.UUID(playlist_id) if playlist_id else None
except: except ValueError:
return request.error_formatter(0, 'Invalid playlist id') return request.error_formatter(0, 'Invalid playlist id')
if playlist_id: if playlist_id:

View File

@ -33,7 +33,7 @@ def old_search():
count = int(count) if count else 20 count = int(count) if count else 20
offset = int(offset) if offset else 0 offset = int(offset) if offset else 0
newer_than = int(newer_than) / 1000 if newer_than else 0 newer_than = int(newer_than) / 1000 if newer_than else 0
except: except ValueError:
return request.error_formatter(0, 'Invalid parameter') return request.error_formatter(0, 'Invalid parameter')
min_date = datetime.fromtimestamp(newer_than) min_date = datetime.fromtimestamp(newer_than)
@ -82,7 +82,7 @@ def new_search():
album_offset = int(album_offset) if album_offset else 0 album_offset = int(album_offset) if album_offset else 0
song_count = int(song_count) if song_count else 20 song_count = int(song_count) if song_count else 20
song_offset = int(song_offset) if song_offset else 0 song_offset = int(song_offset) if song_offset else 0
except: except ValueError:
return request.error_formatter(0, 'Invalid parameter') return request.error_formatter(0, 'Invalid parameter')
if not query: if not query:
@ -111,7 +111,7 @@ def search_id3():
album_offset = int(album_offset) if album_offset else 0 album_offset = int(album_offset) if album_offset else 0
song_count = int(song_count) if song_count else 20 song_count = int(song_count) if song_count else 20
song_offset = int(song_offset) if song_offset else 0 song_offset = int(song_offset) if song_offset else 0
except: except ValueError:
return request.error_formatter(0, 'Invalid parameter') return request.error_formatter(0, 'Invalid parameter')
if not query: if not query:

View File

@ -404,7 +404,7 @@ class Playlist(db.Entity):
tid = UUID(t) tid = UUID(t)
track = Track[tid] track = Track[tid]
tracks.append(track) tracks.append(track)
except: except (ValueError, ObjectNotFound):
should_fix = True should_fix = True
if should_fix: if should_fix:

View File

@ -39,7 +39,7 @@ def playlist_index():
def playlist_details(uid): def playlist_details(uid):
try: try:
uid = uuid.UUID(uid) uid = uuid.UUID(uid)
except: except ValueError:
flash('Invalid playlist id') flash('Invalid playlist id')
return redirect(url_for('playlist_index')) return redirect(url_for('playlist_index'))
@ -56,7 +56,7 @@ def playlist_details(uid):
def playlist_update(uid): def playlist_update(uid):
try: try:
uid = uuid.UUID(uid) uid = uuid.UUID(uid)
except: except ValueError:
flash('Invalid playlist id') flash('Invalid playlist id')
return redirect(url_for('playlist_index')) 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') playlist.public = request.form.get('public') in (True, 'True', 1, '1', 'on', 'checked')
flash('Playlist updated.') flash('Playlist updated.')
return playlist_details(uid) return playlist_details(str(uid))
@app.route('/playlist/del/<uid>') @app.route('/playlist/del/<uid>')
@db_session @db_session
def playlist_delete(uid): def playlist_delete(uid):
try: try:
uid = uuid.UUID(uid) uid = uuid.UUID(uid)
except: except ValueError:
flash('Invalid playlist id') flash('Invalid playlist id')
return redirect(url_for('playlist_index')) return redirect(url_for('playlist_index'))

View File

@ -43,7 +43,7 @@ class FolderManager:
if isinstance(uid, strtype): if isinstance(uid, strtype):
try: try:
uid = uuid.UUID(uid) uid = uuid.UUID(uid)
except: except ValueError:
return FolderManager.INVALID_ID, None return FolderManager.INVALID_ID, None
elif isinstance(uid, uuid.UUID): elif isinstance(uid, uuid.UUID):
pass pass

View File

@ -35,7 +35,7 @@ class UserManager:
if isinstance(uid, strtype): if isinstance(uid, strtype):
try: try:
uid = uuid.UUID(uid) uid = uuid.UUID(uid)
except: except ValueError:
return UserManager.INVALID_ID, None return UserManager.INVALID_ID, None
elif isinstance(uid, uuid.UUID): elif isinstance(uid, uuid.UUID):
pass pass

View File

@ -310,7 +310,7 @@ class Scanner:
if transform: if transform:
value = transform(value) value = transform(value)
return value if value else default return value if value else default
except: except KeyError:
return default return default
def stats(self): def stats(self):

View File

@ -246,7 +246,7 @@ class SupysonicWatcher(object):
try: try:
signal(SIGTERM, self.__terminate) signal(SIGTERM, self.__terminate)
signal(SIGINT, self.__terminate) signal(SIGINT, self.__terminate)
except: except ValueError:
logger.warning('Unable to set signal handlers') logger.warning('Unable to set signal handlers')
queue.start() queue.start()