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:
@ -541,6 +541,12 @@ void modulecmd_arg_free(MODULECMD_ARG* arg)
|
||||
}
|
||||
}
|
||||
|
||||
static void modulecmd_clear_error()
|
||||
{
|
||||
prepare_error();
|
||||
errbuf[0] = '\0';
|
||||
}
|
||||
|
||||
bool modulecmd_call_command(const MODULECMD *cmd, const MODULECMD_ARG *args, json_t** output)
|
||||
{
|
||||
bool rval = false;
|
||||
@ -557,6 +563,8 @@ bool modulecmd_call_command(const MODULECMD *cmd, const MODULECMD_ARG *args, jso
|
||||
args = &MODULECMD_NO_ARGUMENTS;
|
||||
}
|
||||
|
||||
modulecmd_clear_error();
|
||||
|
||||
json_t* discard = NULL;
|
||||
rval = cmd->func(args, output ? output : &discard);
|
||||
json_decref(discard);
|
||||
@ -575,12 +583,6 @@ void modulecmd_set_error(const char *format, ...)
|
||||
va_end(list);
|
||||
}
|
||||
|
||||
static void modulecmd_clear_error()
|
||||
{
|
||||
prepare_error();
|
||||
errbuf[0] = '\0';
|
||||
}
|
||||
|
||||
const char* modulecmd_get_error()
|
||||
{
|
||||
prepare_error();
|
||||
|
@ -1677,13 +1677,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.");
|
||||
}
|
||||
else if (output)
|
||||
|
||||
output = modulecmd_get_json_error();
|
||||
}
|
||||
|
||||
if (output)
|
||||
{
|
||||
char* js = json_dumps(output, JSON_INDENT(4));
|
||||
dcb_printf(dcb, "%s\n", js);
|
||||
|
Reference in New Issue
Block a user