Merge pull request #257 from YunoHost/return-additional-error-data-to-api

Support more complex errors (be able to return additional data in a json structure)
This commit is contained in:
Alexandre Aubin 2021-01-23 16:05:45 +01:00 committed by GitHub
commit ae4087d522
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 3 deletions

View file

@ -377,6 +377,9 @@ class MoulinetteError(Exception):
super(MoulinetteError, self).__init__(msg)
self.strerror = msg
def content(self):
return self.strerror
class MoulinetteLdapIsDownError(MoulinetteError):
"""Used when ldap is down"""

View file

@ -484,7 +484,7 @@ class _ActionsMapPlugin(object):
try:
ret = self.actionsmap.process(arguments, timeout=30, route=_route)
except MoulinetteError as e:
raise HTTPBadRequestResponse(e.strerror)
raise HTTPBadRequestResponse(e)
except Exception as e:
if isinstance(e, HTTPResponse):
raise e
@ -553,8 +553,16 @@ class HTTPOKResponse(HTTPResponse):
class HTTPBadRequestResponse(HTTPResponse):
def __init__(self, output=""):
super(HTTPBadRequestResponse, self).__init__(output, 400)
def __init__(self, error=""):
if isinstance(error, MoulinetteError):
content = error.content()
if isinstance(content, dict):
super(HTTPBadRequestResponse, self).__init__(json_encode(content), 400, headers={'Content-type': 'application/json'})
else:
super(HTTPBadRequestResponse, self).__init__(content, 400)
else:
super(HTTPBadRequestResponse, self).__init__(error, 400)
class HTTPUnauthorizedResponse(HTTPResponse):