MXS-1929: Improve JSON type error messages

If an invalid value or type is given to the REST API, having the expected
type as well as the given type make problem resolution easier.

Also added a value check into MaxCtrl for listener ports.
This commit is contained in:
Markus Mäkelä 2018-07-23 15:30:55 +03:00
parent 0c63471715
commit fb3101f7c4
No known key found for this signature in database
GPG Key ID: 72D48FCE664F7B19
2 changed files with 46 additions and 3 deletions

View File

@ -222,6 +222,10 @@ exports.builder = function(yargs) {
.usage('Usage: create listener <service> <name> <port>')
}, function(argv) {
if (!Number.isInteger(argv.port)) {
throw "'" + argv.port + "' is not a valid value for port"
}
var listener = {
'data': {
'id': argv.name,

View File

@ -1252,6 +1252,45 @@ static inline bool is_null_relation(json_t* json, const char* relation)
return (data && json_is_null(data)) || (base && json_is_null(base));
}
static const char* json_type_to_string(const json_t* json)
{
ss_dassert(json);
if (json_is_object(json))
{
return "an object";
}
else if (json_is_array(json))
{
return "an array";
}
else if (json_is_string(json))
{
return "a string";
}
else if (json_is_integer(json))
{
return "an integer";
}
else if (json_is_real(json))
{
return "a real number";
}
else if (json_is_boolean(json))
{
return "a boolean";
}
else if (json_is_null(json))
{
return "a null value";
}
else
{
ss_dassert(!true);
return "an unknown type";
}
}
static inline const char* get_string_or_null(json_t* json, const char* path)
{
const char* rval = NULL;
@ -1271,7 +1310,7 @@ static inline bool is_string_or_null(json_t* json, const char* path)
if (value && !json_is_string(value))
{
runtime_error("Parameter '%s' is not a string", path);
runtime_error("Parameter '%s' is not a string but %s", path, json_type_to_string(value));
rval = false;
}
@ -1285,7 +1324,7 @@ static inline bool is_bool_or_null(json_t* json, const char* path)
if (value && !json_is_boolean(value))
{
runtime_error("Parameter '%s' is not a boolean", path);
runtime_error("Parameter '%s' is not a boolean but %s", path, json_type_to_string(value));
rval = false;
}
@ -1301,7 +1340,7 @@ static inline bool is_count_or_null(json_t* json, const char* path)
{
if (!json_is_integer(value))
{
runtime_error("Parameter '%s' is not an integer", path);
runtime_error("Parameter '%s' is not an integer but %s", path, json_type_to_string(value));
rval = false;
}
else if (json_integer_value(value) <= 0)