1
0
mirror of https://github.com/spl0k/supysonic.git synced 2024-12-22 17:06:17 +00:00

Adding a function to ease error reporting

This commit is contained in:
Alban 2012-10-20 20:23:38 +02:00
parent 79c219a7d5
commit ec01271dc0
5 changed files with 14 additions and 72 deletions

View File

@ -30,17 +30,14 @@ def set_formatter():
else: else:
request.formatter = ResponseHelper.responsize_xml request.formatter = ResponseHelper.responsize_xml
request.error_formatter = lambda code, msg: request.formatter({ 'error': { 'code': code, 'message': msg } }, error = True)
@app.before_request @app.before_request
def authorize(): def authorize():
if not request.path.startswith('/rest/'): if not request.path.startswith('/rest/'):
return return
error = request.formatter({ error = request.error_formatter(40, 'Unauthorized'), 401
'error': {
'code': 40,
'message': 'Unauthorized'
}
}, error = True), 401
(username, decoded_pass) = map(request.args.get, [ 'u', 'p' ]) (username, decoded_pass) = map(request.args.get, [ 'u', 'p' ])
if not username or not decoded_pass: if not username or not decoded_pass:
@ -73,12 +70,7 @@ def not_found(error):
if not request.path.startswith('/rest/'): if not request.path.startswith('/rest/'):
return error return error
return request.formatter({ return request.error_formatter(0, 'Not implemented'), 501
'error': {
'code': 0,
'message': 'Not implemented'
}
}, error = True), 501
class ResponseHelper: class ResponseHelper:

View File

@ -17,12 +17,7 @@ def rand_songs():
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:
return request.formatter({ return request.error_formatter(0, 'Invalid parameter format')
'error': {
'code': 0,
'message': 'Invalid parameter format'
}
}, error = True)
query = Track.query query = Track.query
if fromYear: if fromYear:

View File

@ -24,12 +24,7 @@ def list_indexes():
try: try:
ifModifiedSince = int(ifModifiedSince) ifModifiedSince = int(ifModifiedSince)
except: except:
return request.formatter({ return request.error_formatter(0, 'Invalid timestamp')
'error': {
'code': 0,
'message': 'Invalid timestamp'
}
}, error = True)
if musicFolderId is None: if musicFolderId is None:
folder = MusicFolder.query.all() folder = MusicFolder.query.all()
@ -37,22 +32,12 @@ def list_indexes():
try: try:
mfid = uuid.UUID(musicFolderId) mfid = uuid.UUID(musicFolderId)
except: except:
return request.formatter({ return request.error_formatter(0, 'Invalid id')
'error': {
'code': 0,
'message': 'Invalid id'
}
}, error = True)
folder = MusicFolder.query.get(mfid) folder = MusicFolder.query.get(mfid)
if not folder: if not folder:
return request.formatter({ return request.error_formatter(70, 'Folder not found')
'error': {
'code': 70,
'message': 'Folder not found'
}
}, error = True)
last_modif = max(map(lambda f: f.last_scan, folder)) if type(folder) is list else folder.last_scan last_modif = max(map(lambda f: f.last_scan, folder)) if type(folder) is list else folder.last_scan
last_modif_ts = int(time.mktime(last_modif.timetuple())) last_modif_ts = int(time.mktime(last_modif.timetuple()))

View File

@ -9,42 +9,22 @@ import os.path, uuid
def stream_media(): def stream_media():
id, maxBitRate, format, timeOffset, size, estimateContentLength = map(request.args.get, [ 'id', 'maxBitRate', 'format', 'timeOffset', 'size', 'estimateContentLength' ]) id, maxBitRate, format, timeOffset, size, estimateContentLength = map(request.args.get, [ 'id', 'maxBitRate', 'format', 'timeOffset', 'size', 'estimateContentLength' ])
if not id: if not id:
return request.formatter({ return request.error_formatter(10, 'Missing media id')
'error': {
'code': 10,
'message': 'Missing media id'
}
}, error = True)
try: try:
tid = uuid.UUID(id) tid = uuid.UUID(id)
except: except:
return request.formatter({ return request.error_formatter(0, 'Invalid media id')
'error': {
'code': 0,
'Message': 'Invalid media id'
}
}, error = True)
track = Track.query.get(tid) track = Track.query.get(tid)
if not track: if not track:
return request.formatter({ return request.error_formatter(70, 'Media not found'), 404
'error': {
'code': 70,
'message': 'Media not found'
}
}, error = True), 404
if maxBitRate: if maxBitRate:
try: try:
maxBitRate = int(maxBitRate) maxBitRate = int(maxBitRate)
except: except:
return request.formatter({ return request.error_formatter(0, 'Invalid bitrate value')
'error': {
'code': 0,
'message': 'Invalid bitrate value'
}
}, error = True)
if track.bitrate > maxBitRate: if track.bitrate > maxBitRate:
# TODO transcode # TODO transcode

View File

@ -8,21 +8,11 @@ from db import User
def user_info(): def user_info():
username = request.args.get('username') username = request.args.get('username')
if username is None: if username is None:
return request.formatter({ return request.error_formatter(10, 'Missing username')
'error': {
'code': 10,
'message': 'Missing username'
}
}, error = True)
user = User.query.filter(User.name == username).first() user = User.query.filter(User.name == username).first()
if user is None: if user is None:
return request.formatter({ return request.error_formatter(0, 'Unknown user')
'error': {
'code': 0,
'message': 'Unkwown user'
}
}, error = True)
return request.formatter({ return request.formatter({
'user': { 'user': {