diff options
Diffstat (limited to 'src/nvim/api')
-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 |
7 files changed, 141 insertions, 78 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; } |