mirror of
https://github.com/neovim/neovim.git
synced 2025-09-25 04:28:33 +00:00
fix(api): generic error messages, not using TRY_WRAP #31596
Problem: - API functions using `try_start` directly, do not surface the underlying error message, and instead show generic messages. - Error-handling code is duplicated in the API impl. - Failure modes are not tested. Solution: - Use `TRY_WRAP`. - Add tests.
This commit is contained in:
@@ -669,16 +669,9 @@ void nvim_set_current_dir(String dir, Error *err)
|
||||
memcpy(string, dir.data, dir.size);
|
||||
string[dir.size] = NUL;
|
||||
|
||||
try_start();
|
||||
|
||||
if (!changedir_func(string, kCdScopeGlobal)) {
|
||||
if (!try_end(err)) {
|
||||
api_set_error(err, kErrorTypeException, "Failed to change directory");
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
try_end(err);
|
||||
TRY_WRAP(err, {
|
||||
changedir_func(string, kCdScopeGlobal);
|
||||
});
|
||||
}
|
||||
|
||||
/// Gets the current line.
|
||||
@@ -942,14 +935,9 @@ void nvim_set_current_win(Window window, Error *err)
|
||||
return;
|
||||
}
|
||||
|
||||
try_start();
|
||||
goto_tabpage_win(win_find_tabpage(win), win);
|
||||
if (!try_end(err) && win != curwin) {
|
||||
api_set_error(err,
|
||||
kErrorTypeException,
|
||||
"Failed to switch to window %d",
|
||||
window);
|
||||
}
|
||||
TRY_WRAP(err, {
|
||||
goto_tabpage_win(win_find_tabpage(win), win);
|
||||
});
|
||||
}
|
||||
|
||||
/// Creates a new, empty, unnamed buffer.
|
||||
@@ -1208,14 +1196,9 @@ void nvim_set_current_tabpage(Tabpage tabpage, Error *err)
|
||||
return;
|
||||
}
|
||||
|
||||
try_start();
|
||||
goto_tabpage_tp(tp, true, true);
|
||||
if (!try_end(err) && tp != curtab) {
|
||||
api_set_error(err,
|
||||
kErrorTypeException,
|
||||
"Failed to switch to tabpage %d",
|
||||
tabpage);
|
||||
}
|
||||
TRY_WRAP(err, {
|
||||
goto_tabpage_tp(tp, true, true);
|
||||
});
|
||||
}
|
||||
|
||||
/// Pastes at cursor (in any mode), and sets "redo" so dot (|.|) will repeat the input. UIs call
|
||||
|
@@ -125,19 +125,17 @@ theend:
|
||||
///
|
||||
/// On execution error: fails with Vimscript error, updates v:errmsg.
|
||||
///
|
||||
/// Prefer using |nvim_cmd()| or |nvim_exec2()| over this. To evaluate multiple lines of Vim script
|
||||
/// or an Ex command directly, use |nvim_exec2()|. To construct an Ex command using a structured
|
||||
/// format and then execute it, use |nvim_cmd()|. To modify an Ex command before evaluating it, use
|
||||
/// |nvim_parse_cmd()| in conjunction with |nvim_cmd()|.
|
||||
/// Prefer |nvim_cmd()| or |nvim_exec2()| instead. To modify an Ex command in a structured way
|
||||
/// before executing it, modify the result of |nvim_parse_cmd()| then pass it to |nvim_cmd()|.
|
||||
///
|
||||
/// @param command Ex command string
|
||||
/// @param[out] err Error details (Vim error), if any
|
||||
void nvim_command(String command, Error *err)
|
||||
FUNC_API_SINCE(1)
|
||||
{
|
||||
try_start();
|
||||
do_cmdline_cmd(command.data);
|
||||
try_end(err);
|
||||
TRY_WRAP(err, {
|
||||
do_cmdline_cmd(command.data);
|
||||
});
|
||||
}
|
||||
|
||||
/// Evaluates a Vimscript |expression|. Dicts and Lists are recursively expanded.
|
||||
@@ -283,13 +281,11 @@ Object nvim_call_dict_function(Object dict, String fn, Array args, Arena *arena,
|
||||
bool mustfree = false;
|
||||
switch (dict.type) {
|
||||
case kObjectTypeString:
|
||||
try_start();
|
||||
if (eval0(dict.data.string.data, &rettv, NULL, &EVALARG_EVALUATE) == FAIL) {
|
||||
api_set_error(err, kErrorTypeException,
|
||||
"Failed to evaluate dict expression");
|
||||
}
|
||||
clear_evalarg(&EVALARG_EVALUATE, NULL);
|
||||
if (try_end(err)) {
|
||||
TRY_WRAP(err, {
|
||||
eval0(dict.data.string.data, &rettv, NULL, &EVALARG_EVALUATE);
|
||||
clear_evalarg(&EVALARG_EVALUATE, NULL);
|
||||
});
|
||||
if (ERROR_SET(err)) {
|
||||
return rv;
|
||||
}
|
||||
// Evaluation of the string arg created a new dict or increased the
|
||||
|
@@ -182,14 +182,9 @@ void nvim_win_set_height(Window window, Integer height, Error *err)
|
||||
return;
|
||||
}
|
||||
|
||||
if (height > INT_MAX || height < INT_MIN) {
|
||||
api_set_error(err, kErrorTypeValidation, "Height value outside range");
|
||||
return;
|
||||
}
|
||||
|
||||
try_start();
|
||||
win_setheight_win((int)height, win);
|
||||
try_end(err);
|
||||
TRY_WRAP(err, {
|
||||
win_setheight_win((int)height, win);
|
||||
});
|
||||
}
|
||||
|
||||
/// Gets the window width
|
||||
@@ -224,14 +219,9 @@ void nvim_win_set_width(Window window, Integer width, Error *err)
|
||||
return;
|
||||
}
|
||||
|
||||
if (width > INT_MAX || width < INT_MIN) {
|
||||
api_set_error(err, kErrorTypeValidation, "Width value outside range");
|
||||
return;
|
||||
}
|
||||
|
||||
try_start();
|
||||
win_setwidth_win((int)width, win);
|
||||
try_end(err);
|
||||
TRY_WRAP(err, {
|
||||
win_setwidth_win((int)width, win);
|
||||
});
|
||||
}
|
||||
|
||||
/// Gets a window-scoped (w:) variable
|
||||
@@ -436,15 +426,15 @@ Object nvim_win_call(Window window, LuaRef fun, Error *err)
|
||||
}
|
||||
tabpage_T *tabpage = win_find_tabpage(win);
|
||||
|
||||
try_start();
|
||||
Object res = OBJECT_INIT;
|
||||
win_execute_T win_execute_args;
|
||||
if (win_execute_before(&win_execute_args, win, tabpage)) {
|
||||
Array args = ARRAY_DICT_INIT;
|
||||
res = nlua_call_ref(fun, NULL, args, kRetLuaref, NULL, err);
|
||||
}
|
||||
win_execute_after(&win_execute_args);
|
||||
try_end(err);
|
||||
TRY_WRAP(err, {
|
||||
win_execute_T win_execute_args;
|
||||
if (win_execute_before(&win_execute_args, win, tabpage)) {
|
||||
Array args = ARRAY_DICT_INIT;
|
||||
res = nlua_call_ref(fun, NULL, args, kRetLuaref, NULL, err);
|
||||
}
|
||||
win_execute_after(&win_execute_args);
|
||||
});
|
||||
return res;
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user