refactor(api): adjust errors for setting options (#23942)

This commit is contained in:
zeertzjq 2023-06-07 09:00:55 +08:00 committed by GitHub
parent b3d5138fd0
commit 0e0a166a0c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 13 additions and 14 deletions

View File

@ -151,24 +151,19 @@ static Object optval_as_object(OptVal o)
} }
/// Consume an API Object and convert it to an OptVal. /// 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) { switch (o.type) {
case kObjectTypeNil: case kObjectTypeNil:
return NIL_OPTVAL; return NIL_OPTVAL;
break;
case kObjectTypeBoolean: case kObjectTypeBoolean:
return BOOLEAN_OPTVAL(o.data.boolean); return BOOLEAN_OPTVAL(o.data.boolean);
break;
case kObjectTypeInteger: case kObjectTypeInteger:
return NUMBER_OPTVAL(o.data.integer); return NUMBER_OPTVAL(o.data.integer);
break;
case kObjectTypeString: case kObjectTypeString:
return STRING_OPTVAL(o.data.string); return STRING_OPTVAL(o.data.string);
break;
default: default:
// Some Object types don't have an OptVal equivalent. Error out in those cases. *error = true;
api_set_error(err, kErrorTypeException, "Invalid option value");
return NIL_OPTVAL; 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. // Handle invalid option value type.
if (ERROR_SET(err)) { if (error) {
api_clear_error(err); // 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), {
VALIDATE_EXP(false, name.data, "Integer/Boolean/String", api_typename(value.type), {
return; return;
}); });
} }

View File

@ -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)) { } else if (!optval_match_type(v, opt_idx)) {
char *rep = optval_to_cstr(v); char *rep = optval_to_cstr(v);
char *valid_types = option_get_valid_types(opt_idx); 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); name, valid_types, optval_type_names[v.type], rep);
xfree(rep); xfree(rep);
xfree(valid_types); xfree(valid_types);

View File

@ -1435,8 +1435,12 @@ describe('API', function()
pcall_err(nvim, 'set_option_value', 'scrolloff', 1, {scope = 'bogus'})) pcall_err(nvim, 'set_option_value', 'scrolloff', 1, {scope = 'bogus'}))
eq("Invalid 'scope': expected String, got Integer", eq("Invalid 'scope': expected String, got Integer",
pcall_err(nvim, 'get_option_value', 'scrolloff', {scope = 42})) 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', {}, {})) 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) end)
it('can get local values when global value is set', function() it('can get local values when global value is set', function()