mirror of
https://github.com/neovim/neovim.git
synced 2025-02-25 18:55:25 -06:00
refactor(api): adjust errors for setting options (#23942)
This commit is contained in:
parent
b3d5138fd0
commit
0e0a166a0c
@ -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;
|
||||
});
|
||||
}
|
||||
|
@ -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);
|
||||
|
@ -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()
|
||||
|
Loading…
Reference in New Issue
Block a user