MXS-1220: Add error descriptions to REST API module commands

The module command calls that fail can now display additional information
in the form of an error object.
This commit is contained in:
Markus Mäkelä 2017-06-09 13:26:22 +03:00
parent 45afbda100
commit 53d7c57982
3 changed files with 41 additions and 1 deletions

View File

@ -265,6 +265,12 @@ void modulecmd_set_error(const char *format, ...);
*/
const char* modulecmd_get_error();
/**
* @brief Get JSON formatted error
*
* @return The error or NULL if no errors have occurred
*/
json_t* modulecmd_get_json_error();
/**
* @brief Call a function for each command

View File

@ -13,6 +13,8 @@
#include <maxscale/modulecmd.h>
#include <string>
#include <maxscale/alloc.h>
#include <maxscale/config.h>
#include <maxscale/pcre2.h>
@ -575,12 +577,39 @@ 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();
return errbuf;
}
json_t* modulecmd_get_json_error()
{
json_t* obj = NULL;
std::string errmsg = modulecmd_get_error();
modulecmd_clear_error();
if (errmsg.length())
{
json_t* err = json_object();
json_object_set_new(err, "detail", json_string(errmsg.c_str()));
json_t* arr = json_array();
json_array_append_new(arr, err);
obj = json_object();
json_object_set_new(obj, "errors", arr);
}
return obj;
}
bool modulecmd_foreach(const char *domain_re, const char *ident_re,
bool(*fn)(const MODULECMD *cmd, void *data), void *data)
{

View File

@ -593,7 +593,7 @@ HttpResponse cb_modulecmd(const HttpRequest& request)
MXS_FREE(opts[i]);
}
int rc = MHD_HTTP_INTERNAL_SERVER_ERROR;
int rc;
if (rval)
{
@ -613,6 +613,11 @@ HttpResponse cb_modulecmd(const HttpRequest& request)
rc = MHD_HTTP_NO_CONTENT;
}
}
else
{
rc = MHD_HTTP_FORBIDDEN;
output = modulecmd_get_json_error();
}
return HttpResponse(rc, output);
}