Always return json object from module commands

If a module command returns a json object, it will always be
returned to the caller, irrespective of whether the command
itself succeeded or not.

Otherwise, if the command failed and if the module command has
set an error message, that error message will be returned as a
json object containing the error message.
This commit is contained in:
Johan Wikman
2017-09-29 09:56:50 +03:00
parent 41938a205f
commit 4aea1b6150
2 changed files with 23 additions and 11 deletions

View File

@ -1679,13 +1679,23 @@ static void callModuleCommand(DCB *dcb, char *domain, char *id, char *v3,
{
json_t* output = NULL;
if (!modulecmd_call_command(cmd, arg, &output))
bool succeeded = modulecmd_call_command(cmd, arg, &output);
if (!succeeded && !output)
{
const char* err = modulecmd_get_error();
dcb_printf(dcb, "Error: %s\n", *err ? err :
"Call to module command failed, see log file for more details");
const char* s = modulecmd_get_error();
ss_dassert(s);
if (*s == 0)
{
// No error had been set, so we add a default one.
modulecmd_set_error("%s", "Call to module command failed, see log file for more details.");
}
output = modulecmd_get_json_error();
}
else if (output)
if (output)
{
char* js = json_dumps(output, JSON_INDENT(4));
dcb_printf(dcb, "%s\n", js);