Merge PR #1199 'Improvements to API error handling'

This commit is contained in:
Thiago de Arruda 2014-09-18 22:37:59 -03:00
commit 53d15c2c25
11 changed files with 201 additions and 146 deletions

View File

@ -125,7 +125,7 @@ ArrayOf(String) buffer_get_slice(Buffer buffer,
int64_t lnum = start + (int64_t)i; int64_t lnum = start + (int64_t)i;
if (lnum > LONG_MAX) { if (lnum > LONG_MAX) {
set_api_error("Line index is too high", err); api_set_error(err, Validation, _("Line index is too high"));
goto end; goto end;
} }
@ -175,7 +175,9 @@ void buffer_set_slice(Buffer buffer,
end = normalize_index(buf, end) + (include_end ? 1 : 0); end = normalize_index(buf, end) + (include_end ? 1 : 0);
if (start > end) { if (start > end) {
set_api_error("start > end", err); api_set_error(err,
Validation,
_("Argument \"start\" is higher than \"end\""));
return; return;
} }
@ -189,7 +191,9 @@ void buffer_set_slice(Buffer buffer,
for (size_t i = 0; i < new_len; i++) { for (size_t i = 0; i < new_len; i++) {
if (replacement.items[i].type != kObjectTypeString) { if (replacement.items[i].type != kObjectTypeString) {
set_api_error("all items in the replacement array must be strings", err); api_set_error(err,
Validation,
_("All items in the replacement array must be strings"));
goto end; goto end;
} }
@ -201,7 +205,7 @@ void buffer_set_slice(Buffer buffer,
switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf); switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
if (u_save((linenr_T)(start - 1), (linenr_T)end) == FAIL) { if (u_save((linenr_T)(start - 1), (linenr_T)end) == FAIL) {
set_api_error("Cannot save undo information", err); api_set_error(err, Exception, _("Failed to save undo information"));
goto end; goto end;
} }
@ -211,7 +215,7 @@ void buffer_set_slice(Buffer buffer,
size_t to_delete = (new_len < old_len) ? (size_t)(old_len - new_len) : 0; size_t to_delete = (new_len < old_len) ? (size_t)(old_len - new_len) : 0;
for (size_t i = 0; i < to_delete; i++) { for (size_t i = 0; i < to_delete; i++) {
if (ml_delete((linenr_T)start, false) == FAIL) { if (ml_delete((linenr_T)start, false) == FAIL) {
set_api_error("Cannot delete line", err); api_set_error(err, Exception, _("Failed to delete line"));
goto end; goto end;
} }
} }
@ -228,12 +232,12 @@ void buffer_set_slice(Buffer buffer,
int64_t lnum = start + (int64_t)i; int64_t lnum = start + (int64_t)i;
if (lnum > LONG_MAX) { if (lnum > LONG_MAX) {
set_api_error("Index value is too high", err); api_set_error(err, Validation, _("Index value is too high"));
goto end; goto end;
} }
if (ml_replace((linenr_T)lnum, (char_u *)lines[i], false) == FAIL) { if (ml_replace((linenr_T)lnum, (char_u *)lines[i], false) == FAIL) {
set_api_error("Cannot replace line", err); api_set_error(err, Exception, _("Failed to replace line"));
goto end; goto end;
} }
// Mark lines that haven't been passed to the buffer as they need // Mark lines that haven't been passed to the buffer as they need
@ -246,12 +250,12 @@ void buffer_set_slice(Buffer buffer,
int64_t lnum = start + (int64_t)i - 1; int64_t lnum = start + (int64_t)i - 1;
if (lnum > LONG_MAX) { if (lnum > LONG_MAX) {
set_api_error("Index value is too high", err); api_set_error(err, Validation, _("Index value is too high"));
goto end; goto end;
} }
if (ml_append((linenr_T)lnum, (char_u *)lines[i], 0, false) == FAIL) { if (ml_append((linenr_T)lnum, (char_u *)lines[i], 0, false) == FAIL) {
set_api_error("Cannot insert line", err); api_set_error(err, Exception, _("Failed to insert line"));
goto end; goto end;
} }
@ -415,7 +419,7 @@ void buffer_set_name(Buffer buffer, String name, Error *err)
} }
if (ren_ret == FAIL) { if (ren_ret == FAIL) {
set_api_error("failed to rename buffer", err); api_set_error(err, Exception, _("Failed to rename buffer"));
} }
} }
@ -425,7 +429,7 @@ void buffer_set_name(Buffer buffer, String name, Error *err)
/// @return true if the buffer is valid, false otherwise /// @return true if the buffer is valid, false otherwise
Boolean buffer_is_valid(Buffer buffer) Boolean buffer_is_valid(Buffer buffer)
{ {
Error stub = {.set = false}; Error stub = ERROR_INIT;
return find_buffer_by_handle(buffer, &stub) != NULL; return find_buffer_by_handle(buffer, &stub) != NULL;
} }
@ -460,7 +464,7 @@ ArrayOf(Integer, 2) buffer_get_mark(Buffer buffer, String name, Error *err)
} }
if (name.size != 1) { if (name.size != 1) {
set_api_error("mark name must be a single character", err); api_set_error(err, Validation, _("Mark name must be a single character"));
return rv; return rv;
} }
@ -478,7 +482,7 @@ ArrayOf(Integer, 2) buffer_get_mark(Buffer buffer, String name, Error *err)
} }
if (posp == NULL) { if (posp == NULL) {
set_api_error("invalid mark name", err); api_set_error(err, Validation, _("Invalid mark name"));
return rv; return rv;
} }

View File

@ -8,6 +8,7 @@
#define ARRAY_DICT_INIT {.size = 0, .capacity = 0, .items = NULL} #define ARRAY_DICT_INIT {.size = 0, .capacity = 0, .items = NULL}
#define STRING_INIT {.data = NULL, .size = 0} #define STRING_INIT {.data = NULL, .size = 0}
#define OBJECT_INIT { .type = kObjectTypeNil } #define OBJECT_INIT { .type = kObjectTypeNil }
#define ERROR_INIT { .set = false }
#define REMOTE_TYPE(type) typedef uint64_t type #define REMOTE_TYPE(type) typedef uint64_t type
#ifdef INCLUDE_GENERATED_DECLARATIONS #ifdef INCLUDE_GENERATED_DECLARATIONS
@ -16,8 +17,14 @@
#endif #endif
// Basic types // Basic types
typedef enum {
kErrorTypeException,
kErrorTypeValidation
} ErrorType;
typedef struct { typedef struct {
char msg[256]; ErrorType type;
char msg[1024];
bool set; bool set;
} Error; } Error;

View File

@ -48,7 +48,7 @@ bool try_end(Error *err)
discard_current_exception(); discard_current_exception();
} }
set_api_error("Keyboard interrupt", err); api_set_error(err, Exception, _("Keyboard interrupt"));
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;
@ -64,7 +64,7 @@ bool try_end(Error *err)
free(msg); free(msg);
} }
} else if (did_throw) { } else if (did_throw) {
set_api_error((char *)current_exception->value, err); api_set_error(err, Exception, "%s", current_exception->value);
} }
return err->set; return err->set;
@ -80,7 +80,7 @@ Object dict_get_value(dict_T *dict, String key, Error *err)
hashitem_T *hi = hash_find(&dict->dv_hashtab, (uint8_t *) key.data); hashitem_T *hi = hash_find(&dict->dv_hashtab, (uint8_t *) key.data);
if (HASHITEM_EMPTY(hi)) { if (HASHITEM_EMPTY(hi)) {
set_api_error("Key not found", err); api_set_error(err, Validation, _("Key not found"));
return (Object) OBJECT_INIT; return (Object) OBJECT_INIT;
} }
@ -101,17 +101,17 @@ Object dict_set_value(dict_T *dict, String key, Object value, Error *err)
Object rv = OBJECT_INIT; Object rv = OBJECT_INIT;
if (dict->dv_lock) { if (dict->dv_lock) {
set_api_error("Dictionary is locked", err); api_set_error(err, Exception, _("Dictionary is locked"));
return rv; return rv;
} }
if (key.size == 0) { if (key.size == 0) {
set_api_error("Empty dictionary keys aren't allowed", err); api_set_error(err, Validation, _("Empty dictionary keys aren't allowed"));
return rv; return rv;
} }
if (key.size > INT_MAX) { if (key.size > INT_MAX) {
set_api_error("Key length is too high", err); api_set_error(err, Validation, _("Key length is too high"));
return rv; return rv;
} }
@ -121,7 +121,7 @@ Object dict_set_value(dict_T *dict, String key, Object value, Error *err)
// Delete the key // Delete the key
if (di == NULL) { if (di == NULL) {
// Doesn't exist, fail // Doesn't exist, fail
set_api_error("Key doesn't exist", err); api_set_error(err, Validation, _("Key \"%s\" doesn't exist"), key.data);
} else { } else {
// Return the old value // Return the old value
rv = vim_to_object(&di->di_tv); rv = vim_to_object(&di->di_tv);
@ -170,7 +170,7 @@ Object get_option_from(void *from, int type, String name, Error *err)
Object rv = OBJECT_INIT; Object rv = OBJECT_INIT;
if (name.size == 0) { if (name.size == 0) {
set_api_error("Empty option name", err); api_set_error(err, Validation, _("Empty option name"));
return rv; return rv;
} }
@ -181,7 +181,10 @@ Object get_option_from(void *from, int type, String name, Error *err)
type, from); type, from);
if (!flags) { if (!flags) {
set_api_error("invalid option name", err); api_set_error(err,
Validation,
_("Invalid option name \"%s\""),
name.data);
return rv; return rv;
} }
@ -197,10 +200,16 @@ Object get_option_from(void *from, int type, String name, Error *err)
rv.data.string.data = stringval; rv.data.string.data = stringval;
rv.data.string.size = strlen(stringval); rv.data.string.size = strlen(stringval);
} else { } else {
set_api_error(N_("Unable to get option value"), err); api_set_error(err,
Exception,
_("Unable to get value for option \"%s\""),
name.data);
} }
} else { } else {
set_api_error(N_("internal error: unknown option type"), err); api_set_error(err,
Exception,
_("Unknown type for option \"%s\""),
name.data);
} }
return rv; return rv;
@ -216,24 +225,33 @@ Object get_option_from(void *from, int type, String name, Error *err)
void set_option_to(void *to, int type, String name, Object value, Error *err) void set_option_to(void *to, int type, String name, Object value, Error *err)
{ {
if (name.size == 0) { if (name.size == 0) {
set_api_error("Empty option name", err); api_set_error(err, Validation, _("Empty option name"));
return; return;
} }
int flags = get_option_value_strict(name.data, NULL, NULL, type, to); int flags = get_option_value_strict(name.data, NULL, NULL, type, to);
if (flags == 0) { if (flags == 0) {
set_api_error("invalid option name", err); api_set_error(err,
Validation,
_("Invalid option name \"%s\""),
name.data);
return; return;
} }
if (value.type == kObjectTypeNil) { if (value.type == kObjectTypeNil) {
if (type == SREQ_GLOBAL) { if (type == SREQ_GLOBAL) {
set_api_error("unable to unset option", err); api_set_error(err,
Exception,
_("Unable to unset option \"%s\""),
name.data);
return; return;
} else if (!(flags & SOPT_GLOBAL)) { } else if (!(flags & SOPT_GLOBAL)) {
set_api_error("cannot unset option that doesn't have a global value", api_set_error(err,
err); Exception,
_("Cannot unset option \"%s\" "
"because it doesn't have a global value"),
name.data);
return; return;
} else { } else {
unset_global_local_option(name.data, to); unset_global_local_option(name.data, to);
@ -245,7 +263,10 @@ void set_option_to(void *to, int type, String name, Object value, Error *err)
if (flags & SOPT_BOOL) { if (flags & SOPT_BOOL) {
if (value.type != kObjectTypeBoolean) { if (value.type != kObjectTypeBoolean) {
set_api_error("option requires a boolean value", err); api_set_error(err,
Validation,
_("Option \"%s\" requires a boolean value"),
name.data);
return; return;
} }
@ -253,12 +274,18 @@ void set_option_to(void *to, int type, String name, Object value, Error *err)
set_option_value_for(name.data, val, NULL, opt_flags, type, to, err); set_option_value_for(name.data, val, NULL, opt_flags, type, to, err);
} else if (flags & SOPT_NUM) { } else if (flags & SOPT_NUM) {
if (value.type != kObjectTypeInteger) { if (value.type != kObjectTypeInteger) {
set_api_error("option requires an integer value", err); api_set_error(err,
Validation,
_("Option \"%s\" requires an integer value"),
name.data);
return; return;
} }
if (value.data.integer > INT_MAX || value.data.integer < INT_MIN) { if (value.data.integer > INT_MAX || value.data.integer < INT_MIN) {
set_api_error("Option value outside range", err); api_set_error(err,
Validation,
_("Value for option \"%s\" is outside range"),
name.data);
return; return;
} }
@ -266,7 +293,10 @@ void set_option_to(void *to, int type, String name, Object value, Error *err)
set_option_value_for(name.data, val, NULL, opt_flags, type, to, err); set_option_value_for(name.data, val, NULL, opt_flags, type, to, err);
} else { } else {
if (value.type != kObjectTypeString) { if (value.type != kObjectTypeString) {
set_api_error("option requires a string value", err); api_set_error(err,
Validation,
_("Option \"%s\" requires a string value"),
name.data);
return; return;
} }
@ -296,7 +326,7 @@ buf_T *find_buffer_by_handle(Buffer buffer, Error *err)
buf_T *rv = handle_get_buffer(buffer); buf_T *rv = handle_get_buffer(buffer);
if (!rv) { if (!rv) {
set_api_error("Invalid buffer id", err); api_set_error(err, Validation, _("Invalid buffer id"));
} }
return rv; return rv;
@ -307,7 +337,7 @@ win_T * find_window_by_handle(Window window, Error *err)
win_T *rv = handle_get_window(window); win_T *rv = handle_get_window(window);
if (!rv) { if (!rv) {
set_api_error("Invalid window id", err); api_set_error(err, Validation, _("Invalid window id"));
} }
return rv; return rv;
@ -318,7 +348,7 @@ tabpage_T * find_tab_by_handle(Tabpage tabpage, Error *err)
tabpage_T *rv = handle_get_tabpage(tabpage); tabpage_T *rv = handle_get_tabpage(tabpage);
if (!rv) { if (!rv) {
set_api_error("Invalid tabpage id", err); api_set_error(err, Validation, _("Invalid tabpage id"));
} }
return rv; return rv;
@ -376,7 +406,7 @@ bool object_to_vim(Object obj, typval_T *tv, Error *err)
case kObjectTypeInteger: case kObjectTypeInteger:
if (obj.data.integer > INT_MAX || obj.data.integer < INT_MIN) { if (obj.data.integer > INT_MAX || obj.data.integer < INT_MIN) {
set_api_error("Integer value outside range", err); api_set_error(err, Validation, _("Integer value outside range"));
return false; return false;
} }
@ -424,7 +454,9 @@ bool object_to_vim(Object obj, typval_T *tv, Error *err)
String key = item.key; String key = item.key;
if (key.size == 0) { if (key.size == 0) {
set_api_error("Empty dictionary keys aren't allowed", err); api_set_error(err,
Validation,
_("Empty dictionary keys aren't allowed"));
// cleanup // cleanup
dict_free(tv->vval.v_dict, true); dict_free(tv->vval.v_dict, true);
return false; return false;
@ -513,6 +545,7 @@ Dictionary api_metadata(void)
if (!metadata.size) { if (!metadata.size) {
msgpack_rpc_init_function_metadata(&metadata); msgpack_rpc_init_function_metadata(&metadata);
init_error_type_metadata(&metadata);
init_type_metadata(&metadata); init_type_metadata(&metadata);
provider_init_feature_metadata(&metadata); provider_init_feature_metadata(&metadata);
} }
@ -520,6 +553,21 @@ Dictionary api_metadata(void)
return copy_object(DICTIONARY_OBJ(metadata)).data.dictionary; return copy_object(DICTIONARY_OBJ(metadata)).data.dictionary;
} }
static void init_error_type_metadata(Dictionary *metadata)
{
Dictionary types = ARRAY_DICT_INIT;
Dictionary exception_metadata = ARRAY_DICT_INIT;
PUT(exception_metadata, "id", INTEGER_OBJ(kErrorTypeException));
Dictionary validation_metadata = ARRAY_DICT_INIT;
PUT(validation_metadata, "id", INTEGER_OBJ(kErrorTypeValidation));
PUT(types, "Exception", DICTIONARY_OBJ(exception_metadata));
PUT(types, "Validation", DICTIONARY_OBJ(validation_metadata));
PUT(*metadata, "error_types", DICTIONARY_OBJ(types));
}
static void init_type_metadata(Dictionary *metadata) static void init_type_metadata(Dictionary *metadata)
{ {
Dictionary types = ARRAY_DICT_INIT; Dictionary types = ARRAY_DICT_INIT;
@ -704,7 +752,9 @@ static void set_option_value_for(char *key,
if (try_end(err)) { if (try_end(err)) {
return; return;
} }
set_api_error("problem while switching windows", err); api_set_error(err,
Exception,
_("Problem while switching windows"));
return; return;
} }
set_option_value_err(key, numval, stringval, opt_flags, err); set_option_value_err(key, numval, stringval, opt_flags, err);
@ -745,6 +795,6 @@ static void set_option_value_err(char *key,
return; return;
} }
set_api_error(errmsg, err); api_set_error(err, Exception, "%s", errmsg);
} }
} }

View File

@ -8,10 +8,13 @@
#include "nvim/memory.h" #include "nvim/memory.h"
#include "nvim/lib/kvec.h" #include "nvim/lib/kvec.h"
#define set_api_error(message, err) \ #define api_set_error(err, errtype, ...) \
do { \ do { \
xstrlcpy(err->msg, message, sizeof(err->msg)); \ snprintf((err)->msg, \
err->set = true; \ sizeof((err)->msg), \
__VA_ARGS__); \
(err)->set = true; \
(err)->type = kErrorType##errtype; \
} while (0) } while (0)
#define OBJECT_OBJ(o) o #define OBJECT_OBJ(o) o

View File

@ -116,7 +116,7 @@ Window tabpage_get_window(Tabpage tabpage, Error *err)
/// @return true if the tab page is valid, false otherwise /// @return true if the tab page is valid, false otherwise
Boolean tabpage_is_valid(Tabpage tabpage) Boolean tabpage_is_valid(Tabpage tabpage)
{ {
Error stub = {.set = false}; Error stub = ERROR_INIT;
return find_tab_by_handle(tabpage, &stub) != NULL; return find_tab_by_handle(tabpage, &stub) != NULL;
} }

View File

@ -1,5 +1,6 @@
#include <assert.h> #include <assert.h>
#include <stdint.h> #include <stdint.h>
#include <inttypes.h>
#include <stdbool.h> #include <stdbool.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@ -117,7 +118,7 @@ Object vim_eval(String str, Error *err)
typval_T *expr_result = eval_expr((char_u *) str.data, NULL); typval_T *expr_result = eval_expr((char_u *) str.data, NULL);
if (!expr_result) { if (!expr_result) {
set_api_error("Failed to eval expression", err); api_set_error(err, Exception, _("Failed to evaluate expression"));
} }
if (!try_end(err)) { if (!try_end(err)) {
@ -139,7 +140,7 @@ Object vim_eval(String str, Error *err)
Integer vim_strwidth(String str, Error *err) Integer vim_strwidth(String str, Error *err)
{ {
if (str.size > INT_MAX) { if (str.size > INT_MAX) {
set_api_error("String length is too high", err); api_set_error(err, Validation, _("String length is too high"));
return 0; return 0;
} }
@ -194,7 +195,7 @@ ArrayOf(String) vim_list_runtime_paths(void)
void vim_change_directory(String dir, Error *err) void vim_change_directory(String dir, Error *err)
{ {
if (dir.size >= MAXPATHL) { if (dir.size >= MAXPATHL) {
set_api_error("directory string is too long", err); api_set_error(err, Validation, _("Directory string is too long"));
return; return;
} }
@ -206,7 +207,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)) {
set_api_error("failed to change directory", err); api_set_error(err, Exception, _("Failed to change directory"));
} }
return; return;
} }
@ -364,18 +365,13 @@ void vim_set_current_buffer(Buffer buffer, Error *err)
} }
try_start(); try_start();
if (do_buffer(DOBUF_GOTO, DOBUF_FIRST, FORWARD, buf->b_fnum, 0) == FAIL) { int result = do_buffer(DOBUF_GOTO, DOBUF_FIRST, FORWARD, buf->b_fnum, 0);
if (try_end(err)) { if (!try_end(err) && result == FAIL) {
return; api_set_error(err,
} Exception,
_("Failed to switch to buffer %" PRIu64),
char msg[256]; buffer);
snprintf(msg, sizeof(msg), "failed to switch to buffer %d", (int)buffer);
set_api_error(msg, err);
return;
} }
try_end(err);
} }
/// Gets the current list of window handles /// Gets the current list of window handles
@ -422,16 +418,12 @@ void vim_set_current_window(Window window, Error *err)
try_start(); try_start();
goto_tabpage_win(win_find_tabpage(win), win); goto_tabpage_win(win_find_tabpage(win), win);
if (!try_end(err) && win != curwin) {
if (win != curwin) { api_set_error(err,
if (try_end(err)) { Exception,
return; _("Failed to switch to window %" PRIu64),
} window);
set_api_error("did not switch to the specified window", err);
return;
} }
try_end(err);
} }
/// Gets the current list of tabpage handles /// Gets the current list of tabpage handles
@ -481,7 +473,12 @@ void vim_set_current_tabpage(Tabpage tabpage, Error *err)
try_start(); try_start();
goto_tabpage_tp(tp, true, true); goto_tabpage_tp(tp, true, true);
try_end(err); if (!try_end(err) && tp != curtab) {
api_set_error(err,
Exception,
_("Failed to switch to tabpage %" PRIu64),
tabpage);
}
} }
/// Subscribes to event broadcasts /// Subscribes to event broadcasts
@ -524,7 +521,7 @@ void vim_register_provider(uint64_t channel_id, String feature, Error *err)
xstrlcpy(buf, feature.data, sizeof(buf)); xstrlcpy(buf, feature.data, sizeof(buf));
if (!provider_register(buf, channel_id)) { if (!provider_register(buf, channel_id)) {
set_api_error("Feature doesn't exist", err); api_set_error(err, Validation, _("Feature doesn't exist"));
} }
} }

View File

@ -57,7 +57,9 @@ void window_set_cursor(Window window, ArrayOf(Integer, 2) pos, Error *err)
if (pos.size != 2 || pos.items[0].type != kObjectTypeInteger || if (pos.size != 2 || pos.items[0].type != kObjectTypeInteger ||
pos.items[1].type != kObjectTypeInteger) { pos.items[1].type != kObjectTypeInteger) {
set_api_error("\"pos\" argument must be a [row, col] array", err); api_set_error(err,
Validation,
_("Argument \"pos\" must be a [row, col] array"));
return; return;
} }
@ -69,12 +71,12 @@ void window_set_cursor(Window window, ArrayOf(Integer, 2) pos, Error *err)
int64_t col = pos.items[1].data.integer; int64_t col = pos.items[1].data.integer;
if (row <= 0 || row > win->w_buffer->b_ml.ml_line_count) { if (row <= 0 || row > win->w_buffer->b_ml.ml_line_count) {
set_api_error("cursor position outside buffer", err); api_set_error(err, Validation, _("Cursor position outside buffer"));
return; return;
} }
if (col > MAXCOL || col < 0) { if (col > MAXCOL || col < 0) {
set_api_error("Column value outside range", err); api_set_error(err, Validation, _("Column value outside range"));
return; return;
} }
@ -117,7 +119,7 @@ void window_set_height(Window window, Integer height, Error *err)
} }
if (height > INT_MAX || height < INT_MIN) { if (height > INT_MAX || height < INT_MIN) {
set_api_error("Height value outside range", err); api_set_error(err, Validation, _("Height value outside range"));
return; return;
} }
@ -160,7 +162,7 @@ void window_set_width(Window window, Integer width, Error *err)
} }
if (width > INT_MAX || width < INT_MIN) { if (width > INT_MAX || width < INT_MIN) {
set_api_error("Width value outside range", err); api_set_error(err, Validation, _("Width value outside range"));
return; return;
} }
@ -283,7 +285,7 @@ Tabpage window_get_tabpage(Window window, Error *err)
/// @return true if the window is valid, false otherwise /// @return true if the window is valid, false otherwise
Boolean window_is_valid(Window window) Boolean window_is_valid(Window window)
{ {
Error stub = {.set = false}; Error stub = ERROR_INIT;
return find_window_by_handle(window, &stub) != NULL; return find_window_by_handle(window, &stub) != NULL;
} }

View File

@ -12471,25 +12471,18 @@ static void f_rpcrequest(typval_T *argvars, typval_T *rettv)
ADD(args, vim_to_object(tv)); ADD(args, vim_to_object(tv));
} }
bool errored; Error err = ERROR_INIT;
Object result; Object result = channel_send_call((uint64_t)argvars[0].vval.v_number,
if (!channel_send_call((uint64_t)argvars[0].vval.v_number, (char *)argvars[1].vval.v_string,
(char *)argvars[1].vval.v_string, args,
args, &err);
&result, if (err.set) {
&errored)) { vim_report_error(cstr_as_string(err.msg));
EMSG2(_(e_invarg2), "Channel doesn't exist");
return;
}
if (errored) {
vim_report_error(result.data.string);
goto end; goto end;
} }
Error conversion_error = {.set = false}; if (!object_to_vim(result, rettv, &err)) {
if (!object_to_vim(result, rettv, &conversion_error)) { EMSG2(_("Error converting the call result: %s"), err.msg);
EMSG(_("Error converting the call result"));
} }
end: end:
@ -19442,7 +19435,7 @@ static void script_host_eval(char *method, typval_T *argvars, typval_T *rettv)
return; return;
} }
Error err = {.set = false}; Error err = ERROR_INIT;
object_to_vim(result, rettv, &err); object_to_vim(result, rettv, &err);
api_free_object(result); api_free_object(result);

View File

@ -172,40 +172,36 @@ bool channel_send_event(uint64_t id, char *name, Array args)
/// Sends a method call to a channel /// Sends a method call to a channel
/// ///
/// @param id The channel id /// @param id The channel id
/// @param name The method name, an arbitrary string /// @param method_name The method name, an arbitrary string
/// @param args Array with method arguments /// @param args Array with method arguments
/// @param[out] result Pointer to return value received from the channel
/// @param[out] error True if the return value is an error /// @param[out] error True if the return value is an error
/// @return True if the call was sent successfully, false otherwise. /// @return Whatever the remote method returned
bool channel_send_call(uint64_t id, Object channel_send_call(uint64_t id,
char *name, char *method_name,
Array args, Array args,
Object *result, Error *err)
bool *errored)
{ {
Channel *channel = NULL; Channel *channel = NULL;
if (!(channel = pmap_get(uint64_t)(channels, id)) || !channel->enabled) { if (!(channel = pmap_get(uint64_t)(channels, id)) || !channel->enabled) {
api_set_error(err, Exception, _("Invalid channel \"%" PRIu64 "\""), id);
api_free_array(args); api_free_array(args);
return false; return NIL;
} }
if (kv_size(channel->call_stack) > 20) { if (kv_size(channel->call_stack) > 20) {
// 20 stack depth is more than anyone should ever need for RPC calls // 20 stack depth is more than anyone should ever need for RPC calls
*errored = true; api_set_error(err,
char buf[256]; Exception,
snprintf(buf, _("Channel %" PRIu64 " crossed maximum stack depth"),
sizeof(buf), channel->id);
"Channel %" PRIu64 " crossed maximum stack depth",
channel->id);
*result = STRING_OBJ(cstr_to_string(buf));
api_free_array(args); api_free_array(args);
return false; return NIL;
} }
uint64_t request_id = channel->next_request_id++; uint64_t request_id = channel->next_request_id++;
// Send the msgpack-rpc request // Send the msgpack-rpc request
send_request(channel, request_id, name, args); send_request(channel, request_id, method_name, args);
EventSource channel_source = channel->is_job EventSource channel_source = channel->is_job
? job_event_source(channel->data.job) ? job_event_source(channel->data.job)
@ -224,10 +220,12 @@ bool channel_send_call(uint64_t id,
channel->enabled && // the channel is still enabled channel->enabled && // the channel is still enabled
kv_size(channel->call_stack) >= size); // the call didn't return kv_size(channel->call_stack) >= size); // the call didn't return
*errored = frame.errored; if (frame.errored) {
*result = frame.result; api_set_error(err, Exception, "%s", frame.result.data.string.data);
return NIL;
}
return true; return frame.result;
} }
/// Subscribes to event broadcasts /// Subscribes to event broadcasts
@ -433,7 +431,9 @@ static bool channel_write(Channel *channel, WBuffer *buffer)
static void send_error(Channel *channel, uint64_t id, char *err) static void send_error(Channel *channel, uint64_t id, char *err)
{ {
channel_write(channel, serialize_response(id, err, NIL, &out_buffer)); Error e = ERROR_INIT;
api_set_error(&e, Exception, "%s", err);
channel_write(channel, serialize_response(id, &e, NIL, &out_buffer));
} }
static void send_request(Channel *channel, static void send_request(Channel *channel,

View File

@ -30,14 +30,14 @@ WBuffer *msgpack_rpc_call(uint64_t channel_id,
FUNC_ATTR_NONNULL_ARG(3) FUNC_ATTR_NONNULL_ARG(3)
{ {
uint64_t response_id; uint64_t response_id;
char *err = msgpack_rpc_validate(&response_id, req); Error error = ERROR_INIT;
msgpack_rpc_validate(&response_id, req, &error);
if (err) { if (error.set) {
return serialize_response(response_id, err, NIL, sbuffer); return serialize_response(response_id, &error, NIL, sbuffer);
} }
// dispatch the call // dispatch the call
Error error = { .set = false };
Object rv = msgpack_rpc_dispatch(channel_id, req, &error); Object rv = msgpack_rpc_dispatch(channel_id, req, &error);
// send the response // send the response
msgpack_packer response; msgpack_packer response;
@ -47,12 +47,12 @@ WBuffer *msgpack_rpc_call(uint64_t channel_id,
ELOG("Error dispatching msgpack-rpc call: %s(request: id %" PRIu64 ")", ELOG("Error dispatching msgpack-rpc call: %s(request: id %" PRIu64 ")",
error.msg, error.msg,
response_id); response_id);
return serialize_response(response_id, error.msg, NIL, sbuffer); return serialize_response(response_id, &error, NIL, sbuffer);
} }
DLOG("Successfully completed mspgack-rpc call(request id: %" PRIu64 ")", DLOG("Successfully completed mspgack-rpc call(request id: %" PRIu64 ")",
response_id); response_id);
return serialize_response(response_id, NULL, rv, sbuffer); return serialize_response(response_id, &error, rv, sbuffer);
} }
/// Finishes the msgpack-rpc call with an error message. /// Finishes the msgpack-rpc call with an error message.
@ -112,10 +112,10 @@ WBuffer *serialize_request(uint64_t request_id,
/// Serializes a msgpack-rpc response /// Serializes a msgpack-rpc response
WBuffer *serialize_response(uint64_t response_id, WBuffer *serialize_response(uint64_t response_id,
char *err_msg, Error *err,
Object arg, Object arg,
msgpack_sbuffer *sbuffer) msgpack_sbuffer *sbuffer)
FUNC_ATTR_NONNULL_ARG(4) FUNC_ATTR_NONNULL_ARG(2, 4)
{ {
msgpack_packer pac; msgpack_packer pac;
msgpack_packer_init(&pac, sbuffer, msgpack_sbuffer_write); msgpack_packer_init(&pac, sbuffer, msgpack_sbuffer_write);
@ -123,11 +123,11 @@ WBuffer *serialize_response(uint64_t response_id,
msgpack_pack_int(&pac, 1); msgpack_pack_int(&pac, 1);
msgpack_pack_uint64(&pac, response_id); msgpack_pack_uint64(&pac, response_id);
if (err_msg) { if (err->set) {
String err = {.size = strlen(err_msg), .data = err_msg}; // error represented by a [type, message] array
// error message msgpack_pack_array(&pac, 2);
msgpack_pack_bin(&pac, err.size); msgpack_rpc_from_integer(err->type, &pac);
msgpack_pack_bin_body(&pac, err.data, err.size); msgpack_rpc_from_string(cstr_as_string(err->msg), &pac);
// Nil result // Nil result
msgpack_pack_nil(&pac); msgpack_pack_nil(&pac);
} else { } else {
@ -146,43 +146,43 @@ WBuffer *serialize_response(uint64_t response_id,
return rv; return rv;
} }
static char *msgpack_rpc_validate(uint64_t *response_id, msgpack_object *req) static void msgpack_rpc_validate(uint64_t *response_id,
msgpack_object *req,
Error *err)
{ {
// response id not known yet // response id not known yet
*response_id = 0; *response_id = 0;
// Validate the basic structure of the msgpack-rpc payload // Validate the basic structure of the msgpack-rpc payload
if (req->type != MSGPACK_OBJECT_ARRAY) { if (req->type != MSGPACK_OBJECT_ARRAY) {
return "Request is not an array"; api_set_error(err, Validation, _("Request is not an array"));
} }
if (req->via.array.size != 4) { if (req->via.array.size != 4) {
return "Request array size should be 4"; api_set_error(err, Validation, _("Request array size should be 4"));
} }
if (req->via.array.ptr[1].type != MSGPACK_OBJECT_POSITIVE_INTEGER) { if (req->via.array.ptr[1].type != MSGPACK_OBJECT_POSITIVE_INTEGER) {
return "Id must be a positive integer"; api_set_error(err, Validation, _("Id must be a positive integer"));
} }
// Set the response id, which is the same as the request // Set the response id, which is the same as the request
*response_id = req->via.array.ptr[1].via.u64; *response_id = req->via.array.ptr[1].via.u64;
if (req->via.array.ptr[0].type != MSGPACK_OBJECT_POSITIVE_INTEGER) { if (req->via.array.ptr[0].type != MSGPACK_OBJECT_POSITIVE_INTEGER) {
return "Message type must be an integer"; api_set_error(err, Validation, _("Message type must be an integer"));
} }
if (req->via.array.ptr[0].via.u64 != 0) { if (req->via.array.ptr[0].via.u64 != 0) {
return "Message type must be 0"; api_set_error(err, Validation, _("Message type must be 0"));
} }
if (req->via.array.ptr[2].type != MSGPACK_OBJECT_BIN if (req->via.array.ptr[2].type != MSGPACK_OBJECT_BIN
&& req->via.array.ptr[2].type != MSGPACK_OBJECT_STR) { && req->via.array.ptr[2].type != MSGPACK_OBJECT_STR) {
return "Method must be a string"; api_set_error(err, Validation, _("Method must be a string"));
} }
if (req->via.array.ptr[3].type != MSGPACK_OBJECT_ARRAY) { if (req->via.array.ptr[3].type != MSGPACK_OBJECT_ARRAY) {
return "Paremeters must be an array"; api_set_error(err, Validation, _("Paremeters must be an array"));
} }
return NULL;
} }

View File

@ -107,12 +107,11 @@ Object provider_call(char *method, Array args)
return NIL; return NIL;
} }
bool error = false; Error err = ERROR_INIT;
Object result = NIL; Object result = NIL = channel_send_call(f->channel_id, method, args, &err);
channel_send_call(f->channel_id, method, args, &result, &error);
if (error) { if (err.set) {
vim_report_error(result.data.string); vim_report_error(cstr_as_string(err.msg));
api_free_object(result); api_free_object(result);
return NIL; return NIL;
} }