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

@ -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();