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

View File

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

View File

@ -5,6 +5,12 @@
#include "api/defs.h" #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 /// Start block that may cause vimscript exceptions
void try_start(void); 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 (vim_chdir((char_u *)string)) {
if (!try_end(err)) { if (!try_end(err)) {
char msg[] = "failed to change directory"; set_api_error("failed to change directory", err);
strncpy(err->msg, msg, sizeof(err->msg));
err->set = true;
} }
return; return;
} }