diff options
author | Thiago de Arruda <tpadilha84@gmail.com> | 2014-09-17 08:56:59 -0300 |
---|---|---|
committer | Thiago de Arruda <tpadilha84@gmail.com> | 2014-09-18 21:30:31 -0300 |
commit | 4a8b52ea08bb5cf501cd20bce4744ae6c7edd9b1 (patch) | |
tree | c7d53772706ed78bee255100b328db11e3dc867e /src | |
parent | 67a16384a46c5eb51ba43d9150e95a1742cffbde (diff) | |
download | rneovim-4a8b52ea08bb5cf501cd20bce4744ae6c7edd9b1.tar.gz rneovim-4a8b52ea08bb5cf501cd20bce4744ae6c7edd9b1.tar.bz2 rneovim-4a8b52ea08bb5cf501cd20bce4744ae6c7edd9b1.zip |
api/msgpack-rpc: Improve error infrastructure
- Add error type information to `Error`
- Rename `set_api_error` to `api_set_error` for consistency with other api_*
functions/macros.
- Refactor the api_set_error macro to accept formatted strings and error types
- Improve error messages
- Wrap error messages with gettext macro
- Refactor msgpack-rpc serialization to transform Error instances into [type,
message] arrays
- Add error type information to API metadata
- Normalize nvim->client and client->nvim error handling(change
channel_send_call to accept an Error pointer instead of the `errored` boolean
pointer)
- Use macro to initialize Error structures
Diffstat (limited to 'src')
-rw-r--r-- | src/nvim/api/buffer.c | 30 | ||||
-rw-r--r-- | src/nvim/api/private/defs.h | 9 | ||||
-rw-r--r-- | src/nvim/api/private/helpers.c | 104 | ||||
-rw-r--r-- | src/nvim/api/private/helpers.h | 11 | ||||
-rw-r--r-- | src/nvim/api/tabpage.c | 2 | ||||
-rw-r--r-- | src/nvim/api/vim.c | 49 | ||||
-rw-r--r-- | src/nvim/api/window.c | 14 | ||||
-rw-r--r-- | src/nvim/eval.c | 29 | ||||
-rw-r--r-- | src/nvim/os/channel.c | 44 | ||||
-rw-r--r-- | src/nvim/os/msgpack_rpc.c | 46 | ||||
-rw-r--r-- | src/nvim/os/provider.c | 9 |
11 files changed, 201 insertions, 146 deletions
diff --git a/src/nvim/api/buffer.c b/src/nvim/api/buffer.c index 8355bfe868..62559163d8 100644 --- a/src/nvim/api/buffer.c +++ b/src/nvim/api/buffer.c @@ -125,7 +125,7 @@ ArrayOf(String) buffer_get_slice(Buffer buffer, int64_t lnum = start + (int64_t)i; 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; } @@ -175,7 +175,9 @@ void buffer_set_slice(Buffer buffer, end = normalize_index(buf, end) + (include_end ? 1 : 0); if (start > end) { - set_api_error("start > end", err); + api_set_error(err, + Validation, + _("Argument \"start\" is higher than \"end\"")); return; } @@ -189,7 +191,9 @@ void buffer_set_slice(Buffer buffer, for (size_t i = 0; i < new_len; i++) { 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; } @@ -201,7 +205,7 @@ void buffer_set_slice(Buffer buffer, switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf); 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; } @@ -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; for (size_t i = 0; i < to_delete; i++) { 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; } } @@ -228,12 +232,12 @@ void buffer_set_slice(Buffer buffer, int64_t lnum = start + (int64_t)i; 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; } 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; } // 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; 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; } 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; } @@ -415,7 +419,7 @@ void buffer_set_name(Buffer buffer, String name, Error *err) } 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 Boolean buffer_is_valid(Buffer buffer) { - Error stub = {.set = false}; + Error stub = ERROR_INIT; 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) { - 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; } @@ -478,7 +482,7 @@ ArrayOf(Integer, 2) buffer_get_mark(Buffer buffer, String name, Error *err) } if (posp == NULL) { - set_api_error("invalid mark name", err); + api_set_error(err, Validation, _("Invalid mark name")); return rv; } diff --git a/src/nvim/api/private/defs.h b/src/nvim/api/private/defs.h index 2dd229ec5f..76ac23a521 100644 --- a/src/nvim/api/private/defs.h +++ b/src/nvim/api/private/defs.h @@ -8,6 +8,7 @@ #define ARRAY_DICT_INIT {.size = 0, .capacity = 0, .items = NULL} #define STRING_INIT {.data = NULL, .size = 0} #define OBJECT_INIT { .type = kObjectTypeNil } +#define ERROR_INIT { .set = false } #define REMOTE_TYPE(type) typedef uint64_t type #ifdef INCLUDE_GENERATED_DECLARATIONS @@ -16,8 +17,14 @@ #endif // Basic types +typedef enum { + kErrorTypeException, + kErrorTypeValidation +} ErrorType; + typedef struct { - char msg[256]; + ErrorType type; + char msg[1024]; bool set; } Error; diff --git a/src/nvim/api/private/helpers.c b/src/nvim/api/private/helpers.c index 14a820aa1b..093cb0e55f 100644 --- a/src/nvim/api/private/helpers.c +++ b/src/nvim/api/private/helpers.c @@ -48,7 +48,7 @@ bool try_end(Error *err) discard_current_exception(); } - set_api_error("Keyboard interrupt", err); + api_set_error(err, Exception, _("Keyboard interrupt")); got_int = false; } else if (msg_list != NULL && *msg_list != NULL) { int should_free; @@ -64,7 +64,7 @@ bool try_end(Error *err) free(msg); } } else if (did_throw) { - set_api_error((char *)current_exception->value, err); + api_set_error(err, Exception, "%s", current_exception->value); } 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); if (HASHITEM_EMPTY(hi)) { - set_api_error("Key not found", err); + api_set_error(err, Validation, _("Key not found")); 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; if (dict->dv_lock) { - set_api_error("Dictionary is locked", err); + api_set_error(err, Exception, _("Dictionary is locked")); return rv; } 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; } 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; } @@ -121,7 +121,7 @@ Object dict_set_value(dict_T *dict, String key, Object value, Error *err) // Delete the key if (di == NULL) { // 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 { // Return the old value 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; if (name.size == 0) { - set_api_error("Empty option name", err); + api_set_error(err, Validation, _("Empty option name")); return rv; } @@ -181,7 +181,10 @@ Object get_option_from(void *from, int type, String name, Error *err) type, from); if (!flags) { - set_api_error("invalid option name", err); + api_set_error(err, + Validation, + _("Invalid option name \"%s\""), + name.data); 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.size = strlen(stringval); } 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 { - set_api_error(N_("internal error: unknown option type"), err); + api_set_error(err, + Exception, + _("Unknown type for option \"%s\""), + name.data); } 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) { if (name.size == 0) { - set_api_error("Empty option name", err); + api_set_error(err, Validation, _("Empty option name")); return; } int flags = get_option_value_strict(name.data, NULL, NULL, type, to); if (flags == 0) { - set_api_error("invalid option name", err); + api_set_error(err, + Validation, + _("Invalid option name \"%s\""), + name.data); return; } if (value.type == kObjectTypeNil) { 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; } else if (!(flags & SOPT_GLOBAL)) { - set_api_error("cannot unset option that doesn't have a global value", - err); + api_set_error(err, + Exception, + _("Cannot unset option \"%s\" " + "because it doesn't have a global value"), + name.data); return; } else { 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 (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; } @@ -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); } else if (flags & SOPT_NUM) { 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; } 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; } @@ -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); } else { 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; } @@ -296,7 +326,7 @@ buf_T *find_buffer_by_handle(Buffer buffer, Error *err) buf_T *rv = handle_get_buffer(buffer); if (!rv) { - set_api_error("Invalid buffer id", err); + api_set_error(err, Validation, _("Invalid buffer id")); } return rv; @@ -307,7 +337,7 @@ win_T * find_window_by_handle(Window window, Error *err) win_T *rv = handle_get_window(window); if (!rv) { - set_api_error("Invalid window id", err); + api_set_error(err, Validation, _("Invalid window id")); } return rv; @@ -318,7 +348,7 @@ tabpage_T * find_tab_by_handle(Tabpage tabpage, Error *err) tabpage_T *rv = handle_get_tabpage(tabpage); if (!rv) { - set_api_error("Invalid tabpage id", err); + api_set_error(err, Validation, _("Invalid tabpage id")); } return rv; @@ -376,7 +406,7 @@ bool object_to_vim(Object obj, typval_T *tv, Error *err) case kObjectTypeInteger: 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; } @@ -424,7 +454,9 @@ bool object_to_vim(Object obj, typval_T *tv, Error *err) String key = item.key; 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 dict_free(tv->vval.v_dict, true); return false; @@ -513,6 +545,7 @@ Dictionary api_metadata(void) if (!metadata.size) { msgpack_rpc_init_function_metadata(&metadata); + init_error_type_metadata(&metadata); init_type_metadata(&metadata); provider_init_feature_metadata(&metadata); } @@ -520,6 +553,21 @@ Dictionary api_metadata(void) 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) { Dictionary types = ARRAY_DICT_INIT; @@ -704,7 +752,9 @@ static void set_option_value_for(char *key, if (try_end(err)) { return; } - set_api_error("problem while switching windows", err); + api_set_error(err, + Exception, + _("Problem while switching windows")); return; } set_option_value_err(key, numval, stringval, opt_flags, err); @@ -745,6 +795,6 @@ static void set_option_value_err(char *key, return; } - set_api_error(errmsg, err); + api_set_error(err, Exception, "%s", errmsg); } } diff --git a/src/nvim/api/private/helpers.h b/src/nvim/api/private/helpers.h index f3ecdaacc4..f29deb53f9 100644 --- a/src/nvim/api/private/helpers.h +++ b/src/nvim/api/private/helpers.h @@ -8,10 +8,13 @@ #include "nvim/memory.h" #include "nvim/lib/kvec.h" -#define set_api_error(message, err) \ - do { \ - xstrlcpy(err->msg, message, sizeof(err->msg)); \ - err->set = true; \ +#define api_set_error(err, errtype, ...) \ + do { \ + snprintf((err)->msg, \ + sizeof((err)->msg), \ + __VA_ARGS__); \ + (err)->set = true; \ + (err)->type = kErrorType##errtype; \ } while (0) #define OBJECT_OBJ(o) o diff --git a/src/nvim/api/tabpage.c b/src/nvim/api/tabpage.c index 91020d6850..72168d936a 100644 --- a/src/nvim/api/tabpage.c +++ b/src/nvim/api/tabpage.c @@ -116,7 +116,7 @@ Window tabpage_get_window(Tabpage tabpage, Error *err) /// @return true if the tab page is valid, false otherwise Boolean tabpage_is_valid(Tabpage tabpage) { - Error stub = {.set = false}; + Error stub = ERROR_INIT; return find_tab_by_handle(tabpage, &stub) != NULL; } diff --git a/src/nvim/api/vim.c b/src/nvim/api/vim.c index 43f2aafdc8..93aa0439f0 100644 --- a/src/nvim/api/vim.c +++ b/src/nvim/api/vim.c @@ -1,5 +1,6 @@ #include <assert.h> #include <stdint.h> +#include <inttypes.h> #include <stdbool.h> #include <stdlib.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); if (!expr_result) { - set_api_error("Failed to eval expression", err); + api_set_error(err, Exception, _("Failed to evaluate expression")); } if (!try_end(err)) { @@ -139,7 +140,7 @@ Object vim_eval(String str, Error *err) Integer vim_strwidth(String str, Error *err) { 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; } @@ -194,7 +195,7 @@ ArrayOf(String) vim_list_runtime_paths(void) void vim_change_directory(String dir, Error *err) { if (dir.size >= MAXPATHL) { - set_api_error("directory string is too long", err); + api_set_error(err, Validation, _("Directory string is too long")); return; } @@ -206,7 +207,7 @@ void vim_change_directory(String dir, Error *err) if (vim_chdir((char_u *)string)) { if (!try_end(err)) { - set_api_error("failed to change directory", err); + api_set_error(err, Exception, _("Failed to change directory")); } return; } @@ -364,18 +365,13 @@ void vim_set_current_buffer(Buffer buffer, Error *err) } try_start(); - if (do_buffer(DOBUF_GOTO, DOBUF_FIRST, FORWARD, buf->b_fnum, 0) == FAIL) { - if (try_end(err)) { - return; - } - - char msg[256]; - snprintf(msg, sizeof(msg), "failed to switch to buffer %d", (int)buffer); - set_api_error(msg, err); - return; + int result = do_buffer(DOBUF_GOTO, DOBUF_FIRST, FORWARD, buf->b_fnum, 0); + if (!try_end(err) && result == FAIL) { + api_set_error(err, + Exception, + _("Failed to switch to buffer %" PRIu64), + buffer); } - - try_end(err); } /// Gets the current list of window handles @@ -422,16 +418,12 @@ void vim_set_current_window(Window window, Error *err) try_start(); goto_tabpage_win(win_find_tabpage(win), win); - - if (win != curwin) { - if (try_end(err)) { - return; - } - set_api_error("did not switch to the specified window", err); - return; + if (!try_end(err) && win != curwin) { + api_set_error(err, + Exception, + _("Failed to switch to window %" PRIu64), + window); } - - try_end(err); } /// Gets the current list of tabpage handles @@ -481,7 +473,12 @@ void vim_set_current_tabpage(Tabpage tabpage, Error *err) try_start(); 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 @@ -524,7 +521,7 @@ void vim_register_provider(uint64_t channel_id, String feature, Error *err) xstrlcpy(buf, feature.data, sizeof(buf)); if (!provider_register(buf, channel_id)) { - set_api_error("Feature doesn't exist", err); + api_set_error(err, Validation, _("Feature doesn't exist")); } } diff --git a/src/nvim/api/window.c b/src/nvim/api/window.c index dd256f2b6d..751518424b 100644 --- a/src/nvim/api/window.c +++ b/src/nvim/api/window.c @@ -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 || 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; } @@ -69,12 +71,12 @@ void window_set_cursor(Window window, ArrayOf(Integer, 2) pos, Error *err) int64_t col = pos.items[1].data.integer; 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; } if (col > MAXCOL || col < 0) { - set_api_error("Column value outside range", err); + api_set_error(err, Validation, _("Column value outside range")); return; } @@ -117,7 +119,7 @@ void window_set_height(Window window, Integer height, Error *err) } 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; } @@ -160,7 +162,7 @@ void window_set_width(Window window, Integer width, Error *err) } 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; } @@ -283,7 +285,7 @@ Tabpage window_get_tabpage(Window window, Error *err) /// @return true if the window is valid, false otherwise Boolean window_is_valid(Window window) { - Error stub = {.set = false}; + Error stub = ERROR_INIT; return find_window_by_handle(window, &stub) != NULL; } diff --git a/src/nvim/eval.c b/src/nvim/eval.c index 760457b54e..a119cd6eac 100644 --- a/src/nvim/eval.c +++ b/src/nvim/eval.c @@ -12471,25 +12471,18 @@ static void f_rpcrequest(typval_T *argvars, typval_T *rettv) ADD(args, vim_to_object(tv)); } - bool errored; - Object result; - if (!channel_send_call((uint64_t)argvars[0].vval.v_number, - (char *)argvars[1].vval.v_string, - args, - &result, - &errored)) { - EMSG2(_(e_invarg2), "Channel doesn't exist"); - return; - } - - if (errored) { - vim_report_error(result.data.string); + Error err = ERROR_INIT; + Object result = channel_send_call((uint64_t)argvars[0].vval.v_number, + (char *)argvars[1].vval.v_string, + args, + &err); + if (err.set) { + vim_report_error(cstr_as_string(err.msg)); goto end; } - - Error conversion_error = {.set = false}; - if (!object_to_vim(result, rettv, &conversion_error)) { - EMSG(_("Error converting the call result")); + + if (!object_to_vim(result, rettv, &err)) { + EMSG2(_("Error converting the call result: %s"), err.msg); } end: @@ -19442,7 +19435,7 @@ static void script_host_eval(char *method, typval_T *argvars, typval_T *rettv) return; } - Error err = {.set = false}; + Error err = ERROR_INIT; object_to_vim(result, rettv, &err); api_free_object(result); diff --git a/src/nvim/os/channel.c b/src/nvim/os/channel.c index 1670424e4e..5598b485ba 100644 --- a/src/nvim/os/channel.c +++ b/src/nvim/os/channel.c @@ -172,40 +172,36 @@ bool channel_send_event(uint64_t id, char *name, Array args) /// Sends a method call to a channel /// /// @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[out] result Pointer to return value received from the channel /// @param[out] error True if the return value is an error -/// @return True if the call was sent successfully, false otherwise. -bool channel_send_call(uint64_t id, - char *name, - Array args, - Object *result, - bool *errored) +/// @return Whatever the remote method returned +Object channel_send_call(uint64_t id, + char *method_name, + Array args, + Error *err) { Channel *channel = NULL; if (!(channel = pmap_get(uint64_t)(channels, id)) || !channel->enabled) { + api_set_error(err, Exception, _("Invalid channel \"%" PRIu64 "\""), id); api_free_array(args); - return false; + return NIL; } if (kv_size(channel->call_stack) > 20) { // 20 stack depth is more than anyone should ever need for RPC calls - *errored = true; - char buf[256]; - snprintf(buf, - sizeof(buf), - "Channel %" PRIu64 " crossed maximum stack depth", - channel->id); - *result = STRING_OBJ(cstr_to_string(buf)); + api_set_error(err, + Exception, + _("Channel %" PRIu64 " crossed maximum stack depth"), + channel->id); api_free_array(args); - return false; + return NIL; } uint64_t request_id = channel->next_request_id++; // 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 ? job_event_source(channel->data.job) @@ -224,10 +220,12 @@ bool channel_send_call(uint64_t id, channel->enabled && // the channel is still enabled kv_size(channel->call_stack) >= size); // the call didn't return - *errored = frame.errored; - *result = frame.result; + if (frame.errored) { + api_set_error(err, Exception, "%s", frame.result.data.string.data); + return NIL; + } - return true; + return frame.result; } /// 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) { - 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, diff --git a/src/nvim/os/msgpack_rpc.c b/src/nvim/os/msgpack_rpc.c index d7e3d33c4b..55bc006ad1 100644 --- a/src/nvim/os/msgpack_rpc.c +++ b/src/nvim/os/msgpack_rpc.c @@ -30,14 +30,14 @@ WBuffer *msgpack_rpc_call(uint64_t channel_id, FUNC_ATTR_NONNULL_ARG(3) { 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) { - return serialize_response(response_id, err, NIL, sbuffer); + if (error.set) { + return serialize_response(response_id, &error, NIL, sbuffer); } // dispatch the call - Error error = { .set = false }; Object rv = msgpack_rpc_dispatch(channel_id, req, &error); // send the 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 ")", error.msg, 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 ")", 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. @@ -112,10 +112,10 @@ WBuffer *serialize_request(uint64_t request_id, /// Serializes a msgpack-rpc response WBuffer *serialize_response(uint64_t response_id, - char *err_msg, + Error *err, Object arg, msgpack_sbuffer *sbuffer) - FUNC_ATTR_NONNULL_ARG(4) + FUNC_ATTR_NONNULL_ARG(2, 4) { msgpack_packer pac; 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_uint64(&pac, response_id); - if (err_msg) { - String err = {.size = strlen(err_msg), .data = err_msg}; - // error message - msgpack_pack_bin(&pac, err.size); - msgpack_pack_bin_body(&pac, err.data, err.size); + if (err->set) { + // error represented by a [type, message] array + msgpack_pack_array(&pac, 2); + msgpack_rpc_from_integer(err->type, &pac); + msgpack_rpc_from_string(cstr_as_string(err->msg), &pac); // Nil result msgpack_pack_nil(&pac); } else { @@ -146,43 +146,43 @@ WBuffer *serialize_response(uint64_t response_id, 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 = 0; // Validate the basic structure of the msgpack-rpc payload 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) { - 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) { - 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 *response_id = req->via.array.ptr[1].via.u64; 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) { - 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 && 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) { - return "Paremeters must be an array"; + api_set_error(err, Validation, _("Paremeters must be an array")); } - - return NULL; } diff --git a/src/nvim/os/provider.c b/src/nvim/os/provider.c index 2e7a677793..d4fffaa053 100644 --- a/src/nvim/os/provider.c +++ b/src/nvim/os/provider.c @@ -107,12 +107,11 @@ Object provider_call(char *method, Array args) return NIL; } - bool error = false; - Object result = NIL; - channel_send_call(f->channel_id, method, args, &result, &error); + Error err = ERROR_INIT; + Object result = NIL = channel_send_call(f->channel_id, method, args, &err); - if (error) { - vim_report_error(result.data.string); + if (err.set) { + vim_report_error(cstr_as_string(err.msg)); api_free_object(result); return NIL; } |