Fix #616: Fixed inconsistencies in subsonic error responses

This commit is contained in:
Eliot Berriot 2018-11-18 23:17:31 +01:00
parent dabe1afd5d
commit 22de4a98c5
No known key found for this signature in database
GPG key ID: DD6965E2476E5C27
5 changed files with 84 additions and 23 deletions

View file

@ -5,23 +5,27 @@ from rest_framework import renderers
import funkwhale_api
def structure_payload(data):
payload = {
"status": "ok",
"version": "1.16.0",
"type": "funkwhale",
"funkwhaleVersion": funkwhale_api.__version__,
}
payload.update(data)
if "detail" in payload:
payload["error"] = {"code": 0, "message": payload.pop("detail")}
if "error" in payload:
payload["status"] = "failed"
return payload
class SubsonicJSONRenderer(renderers.JSONRenderer):
def render(self, data, accepted_media_type=None, renderer_context=None):
if not data:
# when stream view is called, we don't have any data
return super().render(data, accepted_media_type, renderer_context)
final = {
"subsonic-response": {
"status": "ok",
"version": "1.16.0",
"type": "funkwhale",
"funkwhaleVersion": funkwhale_api.__version__,
}
}
final["subsonic-response"].update(data)
if "error" in final:
# an error was returned
final["subsonic-response"]["status"] = "failed"
final = {"subsonic-response": structure_payload(data)}
return super().render(final, accepted_media_type, renderer_context)
@ -32,15 +36,8 @@ class SubsonicXMLRenderer(renderers.JSONRenderer):
if not data:
# when stream view is called, we don't have any data
return super().render(data, accepted_media_type, renderer_context)
final = {
"xmlns": "http://subsonic.org/restapi",
"status": "ok",
"version": "1.16.0",
}
final.update(data)
if "error" in final:
# an error was returned
final["status"] = "failed"
final = structure_payload(data)
final["xmlns"] = "http://subsonic.org/restapi"
tree = dict_to_xml_tree("subsonic-response", final)
return b'<?xml version="1.0" encoding="UTF-8"?>\n' + ET.tostring(
tree, encoding="utf-8"

View file

@ -97,7 +97,10 @@ class SubsonicViewSet(viewsets.GenericViewSet):
def handle_exception(self, exc):
# subsonic API sends 200 status code with custom error
# codes in the payload
mapping = {exceptions.AuthenticationFailed: (40, "Wrong username or password.")}
mapping = {
exceptions.AuthenticationFailed: (40, "Wrong username or password."),
exceptions.NotAuthenticated: (10, "Required parameter is missing."),
}
payload = {"status": "failed"}
if exc.__class__ in mapping:
code, message = mapping[exc.__class__]