Use error messages provided by exception class for P_wrapper

This commit is contained in:
voussoir 2017-03-04 20:07:30 -08:00
parent b9b1879179
commit 901020781c

View file

@ -72,45 +72,40 @@ def delete_synonym(synonym):
def P_wrapper(function): def P_wrapper(function):
def P_wrapped(thingid, response_type='html'): def P_wrapped(thingid, response_type='html'):
ret = function(thingid) try:
if not isinstance(ret, str): return function(thingid)
return ret
if response_type == 'html': except exceptions.EtiquetteException as e:
flask.abort(404, ret) if response_type == 'html':
else: flask.abort(404, e.error_message)
response = jsonify.make_json_response({'error': ret}) else:
flask.abort(response) response = {'error_type': e.error_type, 'error_message': e.error_message}
response = jsonify.make_json_response(response, status=404)
flask.abort(response)
except Exception as e:
if response_type == 'html':
flask.abort(500)
else:
flask.abort(jsonify.make_response({}, status=500))
return P_wrapped return P_wrapped
@P_wrapper @P_wrapper
def P_album(albumid): def P_album(albumid):
try: return P.get_album(albumid)
return P.get_album(albumid)
except exceptions.NoSuchAlbum:
return 'That album doesnt exist'
@P_wrapper @P_wrapper
def P_photo(photoid): def P_photo(photoid):
try: return P.get_photo(photoid)
return P.get_photo(photoid)
except exceptions.NoSuchPhoto:
return 'That photo doesnt exist'
@P_wrapper @P_wrapper
def P_tag(tagname): def P_tag(tagname):
try: return P.get_tag(tagname)
return P.get_tag(tagname)
except exceptions.NoSuchTag as e:
return 'That tag doesnt exist: %s' % tagname
@P_wrapper @P_wrapper
def P_user(username): def P_user(username):
try: return P.get_user(username=username)
return P.get_user(username=username)
except exceptions.NoSuchUser as e:
return 'That user doesnt exist: %s' % e
def send_file(filepath, override_mimetype=None): def send_file(filepath, override_mimetype=None):
''' '''