Support more complex errors (be able to return additional data in a json structure)

This commit is contained in:
Alexandre Aubin 2020-11-22 02:19:56 +01:00
parent fe888dd841
commit bfecb8b7dc
2 changed files with 14 additions and 2 deletions

View file

@ -930,7 +930,7 @@ def app_install(operation_logger, app, label=None, args=None, no_remove_on_failu
permission_sync_to_user()
raise YunohostError(failure_message_with_debug_instructions, raw_msg=True)
raise YunohostError(failure_message_with_debug_instructions, raw_msg=True, log_ref=operation_logger.name)
# Clean hooks and add new ones
hook_remove(app_instance_name)

View file

@ -32,11 +32,23 @@ class YunohostError(MoulinetteError):
are translated via m18n.n (namespace) instead of m18n.g (global?)
"""
def __init__(self, key, raw_msg=False, *args, **kwargs):
def __init__(self, key, raw_msg=False, log_ref=None, *args, **kwargs):
self.key = key # Saving the key is useful for unit testing
self.kwargs = kwargs # Saving the key is useful for unit testing
self.log_ref = log_ref
if raw_msg:
msg = key
else:
msg = m18n.n(key, *args, **kwargs)
super(YunohostError, self).__init__(msg, raw_msg=True)
def content(self):
if not self.log_ref:
return super(YunohostError, self).content()
else:
return {
"error": self.strerror,
"log_ref": self.log_ref
}