fix(api): not using TRY_WRAP, generic error messages #31595

Problem:
- API functions using `try_start` directly instead of `TRY_WRAP`, do not
  surface the underlying error message, and instead show generic things
  like "Failed to set buffer".
- Error handling code is duplicated in the API impl, instead of
  delegating to the vim buffer/window handling logic.

Solution:
- Use `TRY_WRAP`.
This commit is contained in:
Justin M. Keyes
2024-12-16 04:00:20 -08:00
committed by GitHub
parent 9c6a3703bb
commit 167a2383b9
7 changed files with 65 additions and 112 deletions

View File

@@ -746,40 +746,28 @@ void win_set_buf(win_T *win, buf_T *buf, Error *err)
RedrawingDisabled++;
switchwin_T switchwin;
if (switch_win_noblock(&switchwin, win, tab, true) == FAIL) {
api_set_error(err,
kErrorTypeException,
"Failed to switch to window %d",
win->handle);
goto cleanup;
}
try_start();
TRY_WRAP(err, {
int win_result = switch_win_noblock(&switchwin, win, tab, true);
if (win_result != FAIL) {
const int save_acd = p_acd;
if (!switchwin.sw_same_win) {
// Temporarily disable 'autochdir' when setting buffer in another window.
p_acd = false;
}
const int save_acd = p_acd;
if (!switchwin.sw_same_win) {
// Temporarily disable 'autochdir' when setting buffer in another window.
p_acd = false;
}
do_buffer(DOBUF_GOTO, DOBUF_FIRST, FORWARD, buf->b_fnum, 0);
int result = do_buffer(DOBUF_GOTO, DOBUF_FIRST, FORWARD, buf->b_fnum, 0);
if (!switchwin.sw_same_win) {
p_acd = save_acd;
}
if (!try_end(err) && result == FAIL) {
api_set_error(err,
kErrorTypeException,
"Failed to set buffer %d",
buf->handle);
}
if (!switchwin.sw_same_win) {
p_acd = save_acd;
}
}
});
// If window is not current, state logic will not validate its cursor. So do it now.
// Still needed if do_buffer returns FAIL (e.g: autocmds abort script after buffer was set).
validate_cursor(curwin);
cleanup:
restore_win_noblock(&switchwin, true);
RedrawingDisabled--;
}