aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorzeertzjq <zeertzjq@outlook.com>2023-06-07 09:00:55 +0800
committerGitHub <noreply@github.com>2023-06-07 09:00:55 +0800
commit0e0a166a0cc5a2dc199136e313e58c27bfb91977 (patch)
treefe2c40eb718953905ad00c5e931b665d39e9ba35
parentb3d5138fd0066fda26ef7724a542ae45eb42fc84 (diff)
downloadrneovim-0e0a166a0cc5a2dc199136e313e58c27bfb91977.tar.gz
rneovim-0e0a166a0cc5a2dc199136e313e58c27bfb91977.tar.bz2
rneovim-0e0a166a0cc5a2dc199136e313e58c27bfb91977.zip
refactor(api): adjust errors for setting options (#23942)
-rw-r--r--src/nvim/api/options.c19
-rw-r--r--src/nvim/option.c2
-rw-r--r--test/functional/api/vim_spec.lua6
3 files changed, 13 insertions, 14 deletions
diff --git a/src/nvim/api/options.c b/src/nvim/api/options.c
index ed13e51e90..b9a41adc3b 100644
--- a/src/nvim/api/options.c
+++ b/src/nvim/api/options.c
@@ -151,24 +151,19 @@ static Object optval_as_object(OptVal o)
}
/// Consume an API Object and convert it to an OptVal.
-static OptVal object_as_optval(Object o, Error *err)
+static OptVal object_as_optval(Object o, bool *error)
{
switch (o.type) {
case kObjectTypeNil:
return NIL_OPTVAL;
- break;
case kObjectTypeBoolean:
return BOOLEAN_OPTVAL(o.data.boolean);
- break;
case kObjectTypeInteger:
return NUMBER_OPTVAL(o.data.integer);
- break;
case kObjectTypeString:
return STRING_OPTVAL(o.data.string);
- break;
default:
- // Some Object types don't have an OptVal equivalent. Error out in those cases.
- api_set_error(err, kErrorTypeException, "Invalid option value");
+ *error = true;
return NIL_OPTVAL;
}
}
@@ -281,13 +276,13 @@ void nvim_set_option_value(uint64_t channel_id, String name, Object value, Dict(
}
}
- OptVal optval = object_as_optval(value, err);
+ bool error = false;
+ OptVal optval = object_as_optval(value, &error);
// Handle invalid option value type.
- if (ERROR_SET(err)) {
- api_clear_error(err);
-
- VALIDATE_EXP(false, name.data, "Integer/Boolean/String", api_typename(value.type), {
+ if (error) {
+ // Don't use `name` in the error message here, because `name` can be any String.
+ VALIDATE_EXP(false, "value", "Integer/Boolean/String", api_typename(value.type), {
return;
});
}
diff --git a/src/nvim/option.c b/src/nvim/option.c
index bec0f12d68..518d81be49 100644
--- a/src/nvim/option.c
+++ b/src/nvim/option.c
@@ -3719,7 +3719,7 @@ const char *set_option_value(const char *const name, const OptVal value, int opt
} else if (!optval_match_type(v, opt_idx)) {
char *rep = optval_to_cstr(v);
char *valid_types = option_get_valid_types(opt_idx);
- snprintf(errbuf, IOSIZE, _("E5383: Allowed types for option '%s': %s. Got %s value: %s"),
+ snprintf(errbuf, IOSIZE, _("Invalid value for option '%s': expected %s, got %s %s"),
name, valid_types, optval_type_names[v.type], rep);
xfree(rep);
xfree(valid_types);
diff --git a/test/functional/api/vim_spec.lua b/test/functional/api/vim_spec.lua
index 040c26e058..98e5482218 100644
--- a/test/functional/api/vim_spec.lua
+++ b/test/functional/api/vim_spec.lua
@@ -1435,8 +1435,12 @@ describe('API', function()
pcall_err(nvim, 'set_option_value', 'scrolloff', 1, {scope = 'bogus'}))
eq("Invalid 'scope': expected String, got Integer",
pcall_err(nvim, 'get_option_value', 'scrolloff', {scope = 42}))
- eq("Invalid 'scrolloff': expected Integer/Boolean/String, got Array",
+ eq("Invalid 'value': expected Integer/Boolean/String, got Array",
pcall_err(nvim, 'set_option_value', 'scrolloff', {}, {}))
+ eq("Invalid value for option 'scrolloff': expected Number, got Boolean true",
+ pcall_err(nvim, 'set_option_value', 'scrolloff', true, {}))
+ eq("Invalid value for option 'scrolloff': expected Number, got String \"wrong\"",
+ pcall_err(nvim, 'set_option_value', 'scrolloff', 'wrong', {}))
end)
it('can get local values when global value is set', function()