mirror of
https://github.com/YunoHost/moulinette.git
synced 2024-09-03 20:06:31 +02:00
Support more complex errors (be able to return additional data in a json structure)
This commit is contained in:
parent
f01466d6ef
commit
dfb88418f1
2 changed files with 14 additions and 3 deletions
|
@ -388,6 +388,9 @@ class MoulinetteError(Exception):
|
||||||
super(MoulinetteError, self).__init__(msg)
|
super(MoulinetteError, self).__init__(msg)
|
||||||
self.strerror = msg
|
self.strerror = msg
|
||||||
|
|
||||||
|
def content(self):
|
||||||
|
return self.strerror
|
||||||
|
|
||||||
|
|
||||||
class MoulinetteLdapIsDownError(MoulinetteError):
|
class MoulinetteLdapIsDownError(MoulinetteError):
|
||||||
"""Used when ldap is down"""
|
"""Used when ldap is down"""
|
||||||
|
|
|
@ -481,7 +481,7 @@ class _ActionsMapPlugin(object):
|
||||||
try:
|
try:
|
||||||
ret = self.actionsmap.process(arguments, timeout=30, route=_route)
|
ret = self.actionsmap.process(arguments, timeout=30, route=_route)
|
||||||
except MoulinetteError as e:
|
except MoulinetteError as e:
|
||||||
raise HTTPBadRequestResponse(e.strerror)
|
raise HTTPBadRequestResponse(e)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
if isinstance(e, HTTPResponse):
|
if isinstance(e, HTTPResponse):
|
||||||
raise e
|
raise e
|
||||||
|
@ -550,8 +550,16 @@ class HTTPOKResponse(HTTPResponse):
|
||||||
|
|
||||||
|
|
||||||
class HTTPBadRequestResponse(HTTPResponse):
|
class HTTPBadRequestResponse(HTTPResponse):
|
||||||
def __init__(self, output=""):
|
def __init__(self, error=""):
|
||||||
super(HTTPBadRequestResponse, self).__init__(output, 400)
|
|
||||||
|
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):
|
class HTTPUnauthorizedResponse(HTTPResponse):
|
||||||
|
|
Loading…
Add table
Reference in a new issue