1
0
mirror of https://github.com/spl0k/supysonic.git synced 2024-12-23 01:16:18 +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:
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 *

View File

@ -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()

View File

@ -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())

View File

@ -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:

View File

@ -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:

View File

@ -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'))

View File

@ -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:

View File

@ -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:

View File

@ -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:

View File

@ -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/<uid>')
@db_session
def playlist_delete(uid):
try:
uid = uuid.UUID(uid)
except:
except ValueError:
flash('Invalid playlist id')
return redirect(url_for('playlist_index'))

View File

@ -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

View File

@ -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

View File

@ -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):

View File

@ -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()