Now return proper error payload on subsonic API

merge-requests/237/head
Eliot Berriot 2018-05-10 17:31:49 +02:00
rodzic f5225d3285
commit 6f79dd475d
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: DD6965E2476E5C27
2 zmienionych plików z 29 dodań i 16 usunięć

Wyświetl plik

@ -15,6 +15,9 @@ class SubsonicJSONRenderer(renderers.JSONRenderer):
}
}
final['subsonic-response'].update(data)
if 'error' in final:
# an error was returned
final['subsonic-response']['status'] = 'failed'
return super().render(final, accepted_media_type, renderer_context)
@ -31,6 +34,9 @@ class SubsonicXMLRenderer(renderers.JSONRenderer):
'version': '1.16.0',
}
final.update(data)
if 'error' in final:
# an error was returned
final['status'] = 'failed'
tree = dict_to_xml_tree('subsonic-response', final)
return b'<?xml version="1.0" encoding="UTF-8"?>\n' + ET.tostring(tree, encoding='utf-8')

Wyświetl plik

@ -31,15 +31,19 @@ def find_object(queryset, model_field='pk', field='id', cast=int):
raw_value = data[field]
except KeyError:
return response.Response({
'code': 10,
'message': "required parameter '{}' not present".format(field)
'error': {
'code': 10,
'message': "required parameter '{}' not present".format(field)
}
})
try:
value = cast(raw_value)
except (TypeError, ValidationError):
return response.Response({
'code': 0,
'message': 'For input string "{}"'.format(raw_value)
'error': {
'code': 0,
'message': 'For input string "{}"'.format(raw_value)
}
})
qs = queryset
if hasattr(qs, '__call__'):
@ -48,9 +52,11 @@ def find_object(queryset, model_field='pk', field='id', cast=int):
obj = qs.get(**{model_field: value})
except qs.model.DoesNotExist:
return response.Response({
'code': 70,
'message': '{} not found'.format(
qs.model.__class__.__name__)
'error': {
'code': 70,
'message': '{} not found'.format(
qs.model.__class__.__name__)
}
})
kwargs['obj'] = obj
return func(self, request, *args, **kwargs)
@ -83,15 +89,14 @@ class SubsonicViewSet(viewsets.GenericViewSet):
payload = {
'status': 'failed'
}
try:
if exc.__class__ in mapping:
code, message = mapping[exc.__class__]
except KeyError:
return super().handle_exception(exc)
else:
payload['error'] = {
'code': code,
'message': message
}
return super().handle_exception(exc)
payload['error'] = {
'code': code,
'message': message
}
return response.Response(payload, status=200)
@ -450,8 +455,10 @@ class SubsonicViewSet(viewsets.GenericViewSet):
name = data.get('name', '')
if not name:
return response.Response({
'code': 10,
'message': 'Playlist ID or name must be specified.'
'error': {
'code': 10,
'message': 'Playlist ID or name must be specified.'
}
}, data)
playlist = request.user.playlists.create(