API: Extract error boilerplate into a macro

This commit is contained in:
Thiago de Arruda 2014-05-08 17:24:14 -03:00
parent 9f25a4153c
commit d488b7de1d
4 changed files with 15 additions and 27 deletions

View File

@ -68,9 +68,7 @@ void buffer_set_line(Buffer buffer, int64_t index, Object line, Error *err)
}
if (line.type != kObjectTypeNil && line.type != kObjectTypeString) {
char msg[] = "Invalid line";
strncpy(err->msg, msg, sizeof(err->msg));
err->set = true;
set_api_error("Invalid line", err);
return;
}
@ -86,14 +84,10 @@ void buffer_set_line(Buffer buffer, int64_t index, Object line, Error *err)
if (u_savedel(index, 1L) == FAIL) {
// Failed to save undo
char msg[] = "Cannot save undo information";
strncpy(err->msg, msg, sizeof(err->msg));
err->set = true;
set_api_error("Cannot save undo information", err);
} else if (ml_delete(index, FALSE) == FAIL) {
// Failed to delete
char msg[] = "Cannot delete the line";
strncpy(err->msg, msg, sizeof(err->msg));
err->set = true;
set_api_error("Cannot delete the line", err);
} else {
restore_win_for_buf(save_curwin, save_curtab, save_curbuf);
// Success
@ -117,14 +111,10 @@ void buffer_set_line(Buffer buffer, int64_t index, Object line, Error *err)
if (u_savesub(index) == FAIL) {
// Failed to save undo
char msg[] = "Cannot save undo information";
strncpy(err->msg, msg, sizeof(err->msg));
err->set = true;
set_api_error("Cannot save undo information", err);
} else if (ml_replace(index, (char_u *)string, FALSE) == FAIL) {
// Failed to replace
char msg[] = "Cannot replace line";
strncpy(err->msg, msg, sizeof(err->msg));
err->set = true;
set_api_error("Cannot replace line", err);
free(string);
} else {
// Success
@ -213,9 +203,7 @@ static buf_T *find_buffer(Buffer buffer, Error *err)
buf_T *buf = buflist_findnr(buffer);
if (buf == NULL) {
char msg[] = "Invalid buffer id";
strncpy(err->msg, msg, sizeof(err->msg));
err->set = true;
set_api_error("Invalid buffer id", err);
}
return buf;

View File

@ -21,15 +21,12 @@ bool try_end(Error *err)
did_emsg = false;
if (got_int) {
const char msg[] = "Keyboard interrupt";
if (did_throw) {
// If we got an interrupt, discard the current exception
discard_current_exception();
}
strncpy(err->msg, msg, sizeof(err->msg));
err->set = true;
set_api_error("Keyboard interrupt", err);
got_int = false;
} else if (msg_list != NULL && *msg_list != NULL) {
int should_free;
@ -45,8 +42,7 @@ bool try_end(Error *err)
free(msg);
}
} else if (did_throw) {
strncpy(err->msg, (char *)current_exception->value, sizeof(err->msg));
err->set = true;
set_api_error((char *)current_exception->value, err);
}
return err->set;

View File

@ -5,6 +5,12 @@
#include "api/defs.h"
#define set_api_error(message, err) \
do { \
strncpy(err->msg, message, sizeof(err->msg)); \
err->set = true; \
} while (0)
/// Start block that may cause vimscript exceptions
void try_start(void);

View File

@ -127,9 +127,7 @@ void vim_change_directory(String dir, Error *err)
if (vim_chdir((char_u *)string)) {
if (!try_end(err)) {
char msg[] = "failed to change directory";
strncpy(err->msg, msg, sizeof(err->msg));
err->set = true;
set_api_error("failed to change directory", err);
}
return;
}