Added proper handling of internal errors (should be no 500s anywhere)

This commit is contained in:
Kristy Fournier 2026-01-26 12:30:08 -05:00
parent 2002dd1afa
commit fce09edfc5
2 changed files with 75 additions and 61 deletions

View file

@ -38,7 +38,6 @@ async function getFromServer(bodyInfo, source="",password=adminPass) {
// the currently set password is always included in every request // the currently set password is always included in every request
bodyInfo["password"] = password; bodyInfo["password"] = password;
} }
// console.log(bodyInfo);
const response = await fetch("http://"+ip+"/"+source, { const response = await fetch("http://"+ip+"/"+source, {
method: "POST", method: "POST",
body: JSON.stringify(bodyInfo), body: JSON.stringify(bodyInfo),

View file

@ -13,6 +13,7 @@ portTheUserPicked=os.getenv("SERVER_PORT")
ERR_NO_ADMIN = ({"error":"no-admin","data":None},401) ERR_NO_ADMIN = ({"error":"no-admin","data":None},401)
ERR_200 = ({"error":"OK","data":None},200) ERR_200 = ({"error":"OK","data":None},200)
ERR_MISSING_ARGS = ({"error":"Request missing required arguments","data":None}),400
if args.admin: if args.admin:
ADMIN_PASS = hashlib.sha256(bytes(args.admin,'utf-8')).hexdigest() ADMIN_PASS = hashlib.sha256(bytes(args.admin,'utf-8')).hexdigest()
else: else:
@ -108,7 +109,7 @@ def playerControls():
global skipNow global skipNow
global partyMode global partyMode
recieveData=request.get_json(force=True) recieveData=request.get_json(force=True)
if recieveData["control"] != None: try:
if recieveData["control"] == "play-pause": if recieveData["control"] == "play-pause":
if ADMIN_PASS == recieveData['password'] or controlPerms["PP"]: if ADMIN_PASS == recieveData['password'] or controlPerms["PP"]:
player.pause() player.pause()
@ -131,8 +132,8 @@ def playerControls():
return ERR_NO_ADMIN return ERR_NO_ADMIN
else: else:
return {"error":"Not a valid control","data":None},400 return {"error":"Not a valid control","data":None},400
else: except KeyError:
return {"error":"No control sent","data":None},400 return ERR_MISSING_ARGS
@app.route("/settings", methods=['POST']) @app.route("/settings", methods=['POST'])
def settingsControl(): def settingsControl():
@ -141,6 +142,7 @@ def settingsControl():
global partyMode global partyMode
global player global player
recieveData = request.get_json(force=True) recieveData = request.get_json(force=True)
try:
if recieveData["setting"] == "volume": if recieveData["setting"] == "volume":
if ADMIN_PASS == recieveData['password'] or controlPerms["VOL"]: if ADMIN_PASS == recieveData['password'] or controlPerms["VOL"]:
volumeLevel = int(recieveData["level"]) volumeLevel = int(recieveData["level"])
@ -167,13 +169,16 @@ def settingsControl():
# probably should have made this a different request type or something but it works # probably should have made this a different request type or something but it works
return {"error":"ok","data":{"partymode":partyMode,"volume":player.audio_get_volume(),"admin":controlPerms}},200 return {"error":"ok","data":{"partymode":partyMode,"volume":player.audio_get_volume(),"admin":controlPerms}},200
else: else:
return {"error":"Not a valid setting","data":None},422 return {"error":"Not a valid setting","data":None},400
except:
return ERR_MISSING_ARGS
@app.route("/search", methods=['POST']) @app.route("/search", methods=['POST'])
def searchSongDB(): def searchSongDB():
recieveData=request.get_json(force=True) recieveData=request.get_json(force=True)
fileofDB = sql.connect("songDatabase.db") fileofDB = sql.connect("songDatabase.db")
songDatabase = fileofDB.cursor() songDatabase = fileofDB.cursor()
try:
results = [] results = []
if (recieveData['search'] == ""): if (recieveData['search'] == ""):
songDatabase.execute("SELECT * FROM virtualSongs") songDatabase.execute("SELECT * FROM virtualSongs")
@ -194,10 +199,18 @@ def searchSongDB():
fileofDB.close() fileofDB.close()
return {"error":"ok","data":tempdata},200 return {"error":"ok","data":tempdata},200
except KeyError:
fileofDB.close()
return ERR_MISSING_ARGS
except sql.OperationalError:
fileofDB.close()
return ({"error":"Invalid search, sorry!","data":None},422)
@app.route("/songadd", methods=["POST"]) @app.route("/songadd", methods=["POST"])
def songadd(): def songadd():
recieveData=request.get_json(force=True) recieveData=request.get_json(force=True)
try:
if (ADMIN_PASS == recieveData['password']) or controlPerms["AS"]: if (ADMIN_PASS == recieveData['password']) or controlPerms["AS"]:
# Password exists and is correct, or it's not restricted # Password exists and is correct, or it's not restricted
# if (recieveData['song'] in playlist): # if (recieveData['song'] in playlist):
@ -211,6 +224,8 @@ def songadd():
else: else:
# the password is incorrect (technically a password not existing falls into the above case because controlPerms is never changed) # the password is incorrect (technically a password not existing falls into the above case because controlPerms is never changed)
return ERR_NO_ADMIN return ERR_NO_ADMIN
except KeyError:
return ERR_MISSING_ARGS
@app.route("/playlist", methods=["POST"]) @app.route("/playlist", methods=["POST"])
def getPlaylist(): def getPlaylist():