Add mxs_json_error_append
Using this function, more error object can be added to the error array of an json error object.
This commit is contained in:
parent
438b4e0341
commit
e41f60fd2e
@ -116,4 +116,15 @@ json_t* mxs_json_pointer(json_t* json, const char* json_ptr);
|
||||
*/
|
||||
json_t* mxs_json_error(const char* format, ...);
|
||||
|
||||
/**
|
||||
* @brief Append error to existing JSON object.
|
||||
*
|
||||
* @param object Existing json error object, or NULL.
|
||||
* @param format Format string
|
||||
* @param ... Variable argument list
|
||||
*
|
||||
* @return The error added to 'errors' array of the JSON object.
|
||||
*/
|
||||
json_t* mxs_json_error_append(json_t* object, const char* format, ...);
|
||||
|
||||
MXS_END_DECLS
|
||||
|
@ -20,6 +20,14 @@
|
||||
|
||||
using std::string;
|
||||
|
||||
namespace
|
||||
{
|
||||
|
||||
const char DETAIL[] = "detail";
|
||||
const char ERRORS[] = "errors";
|
||||
|
||||
}
|
||||
|
||||
static json_t* self_link(const char* host, const char* endpoint)
|
||||
{
|
||||
json_t* self_link = json_object();
|
||||
@ -164,6 +172,26 @@ json_t* mxs_json_self_link(const char* host, const char* path, const char* id)
|
||||
return links;
|
||||
}
|
||||
|
||||
static json_t* json_error_detail(const char* message)
|
||||
{
|
||||
json_t* err = json_object();
|
||||
json_object_set_new(err, DETAIL, json_string(message));
|
||||
return err;
|
||||
}
|
||||
|
||||
static json_t* json_error(const char* message)
|
||||
{
|
||||
json_t* err = json_error_detail(message);
|
||||
|
||||
json_t* arr = json_array();
|
||||
json_array_append_new(arr, err);
|
||||
|
||||
json_t* obj = json_object();
|
||||
json_object_set_new(obj, ERRORS, arr);
|
||||
|
||||
return obj;
|
||||
}
|
||||
|
||||
json_t* mxs_json_error(const char* format, ...)
|
||||
{
|
||||
va_list args;
|
||||
@ -177,14 +205,46 @@ json_t* mxs_json_error(const char* format, ...)
|
||||
vsnprintf(message, sizeof(message), format, args);
|
||||
va_end(args);
|
||||
|
||||
json_t* err = json_object();
|
||||
json_object_set_new(err, "detail", json_string(message));
|
||||
return json_error(message);
|
||||
}
|
||||
|
||||
json_t* arr = json_array();
|
||||
json_array_append_new(arr, err);
|
||||
static json_t* json_error_append(json_t* obj, const char* message)
|
||||
{
|
||||
json_t* err = json_error_detail(message);
|
||||
|
||||
json_t* obj = json_object();
|
||||
json_object_set_new(obj, "errors", arr);
|
||||
json_t* arr = json_object_get(obj, ERRORS);
|
||||
ss_dassert(arr);
|
||||
ss_dassert(json_is_array(arr));
|
||||
|
||||
if (arr)
|
||||
{
|
||||
json_array_append_new(arr, err);
|
||||
}
|
||||
|
||||
return obj;
|
||||
}
|
||||
|
||||
json_t* mxs_json_error_append(json_t* obj, const char* format, ...)
|
||||
{
|
||||
va_list args;
|
||||
|
||||
va_start(args, format);
|
||||
int len = vsnprintf(NULL, 0, format, args);
|
||||
va_end(args);
|
||||
|
||||
char message[len + 1];
|
||||
va_start(args, format);
|
||||
vsnprintf(message, sizeof(message), format, args);
|
||||
va_end(args);
|
||||
|
||||
if (!obj)
|
||||
{
|
||||
obj = json_error(message);
|
||||
}
|
||||
else
|
||||
{
|
||||
obj = json_error_append(obj, message);
|
||||
}
|
||||
|
||||
return obj;
|
||||
}
|
||||
|
@ -287,11 +287,47 @@ int test1()
|
||||
ss_dassert(json_is_array(mxs_json_pointer(json, "data/0/attributes/slaves")));
|
||||
ss_dassert(json_array_size(mxs_json_pointer(json, "data/0/attributes/slaves")) == 3);
|
||||
|
||||
json_decref(json);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int test2()
|
||||
{
|
||||
char *s;
|
||||
json_t* err;
|
||||
|
||||
err = mxs_json_error("%s", "This is an error!");
|
||||
s = json_dumps(err, 0);
|
||||
printf("%s\n", s);
|
||||
ss_dassert(strcmp(s, "{\"errors\": [{\"detail\": \"This is an error!\"}]}") == 0);
|
||||
MXS_FREE(s);
|
||||
|
||||
json_decref(err);
|
||||
|
||||
err = mxs_json_error_append(NULL, "%s", "This is an error!");
|
||||
s = json_dumps(err, 0);
|
||||
printf("%s\n", s);
|
||||
ss_dassert(strcmp(s, "{\"errors\": [{\"detail\": \"This is an error!\"}]}") == 0);
|
||||
MXS_FREE(s);
|
||||
|
||||
err = mxs_json_error_append(err, "%s", "This is another error!");
|
||||
s = json_dumps(err, 0);
|
||||
printf("%s\n", s);
|
||||
ss_dassert(strcmp(s,
|
||||
"{\"errors\": [{\"detail\": \"This is an error!\"}, "
|
||||
"{\"detail\": \"This is another error!\"}]}") == 0);
|
||||
MXS_FREE(s);
|
||||
|
||||
json_decref(err);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
test1();
|
||||
int errors = 0;
|
||||
errors += test1();
|
||||
errors += test2();
|
||||
return 0;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user