From 01c412c4607911f20086264ddf4ac269f3b8eb5f Mon Sep 17 00:00:00 2001 From: Jo-Philipp Wich Date: Fri, 9 Sep 2022 13:59:27 +0200 Subject: ubus: add toplevel constants for ubus status codes Add constants for ubus status codes to the toplevel module scope in order to avoid the need for magic values in the code. Suggested-by: Felix Fietkau Signed-off-by: Jo-Philipp Wich --- lib/ubus.c | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) (limited to 'lib/ubus.c') diff --git a/lib/ubus.c b/lib/ubus.c index e2491ec..a9bafcb 100644 --- a/lib/ubus.c +++ b/lib/ubus.c @@ -2053,6 +2053,25 @@ void uc_module_init(uc_vm_t *vm, uc_value_t *scope) { uc_function_list_register(scope, global_fns); +#define ADD_CONST(x) ucv_object_add(scope, #x, ucv_int64_new(UBUS_##x)) + ADD_CONST(STATUS_OK); + ADD_CONST(STATUS_INVALID_COMMAND); + ADD_CONST(STATUS_INVALID_ARGUMENT); + ADD_CONST(STATUS_METHOD_NOT_FOUND); + ADD_CONST(STATUS_NOT_FOUND); + ADD_CONST(STATUS_NO_DATA); + ADD_CONST(STATUS_PERMISSION_DENIED); + ADD_CONST(STATUS_TIMEOUT); + ADD_CONST(STATUS_NOT_SUPPORTED); + ADD_CONST(STATUS_UNKNOWN_ERROR); + ADD_CONST(STATUS_CONNECTION_FAILED); + +#ifdef HAVE_NEW_UBUS_STATUS_CODES + ADD_CONST(STATUS_NO_MEMORY); + ADD_CONST(STATUS_PARSE_ERROR); + ADD_CONST(STATUS_SYSTEM_ERROR); +#endif + conn_type = uc_type_declare(vm, "ubus.connection", conn_fns, free_connection); defer_type = uc_type_declare(vm, "ubus.deferred", defer_fns, free_deferred); object_type = uc_type_declare(vm, "ubus.object", object_fns, free_object); -- cgit v1.2.3 From cc4eb798c1bb55ce82698e548dc4e981a7d8a1ce Mon Sep 17 00:00:00 2001 From: Jo-Philipp Wich Date: Fri, 9 Sep 2022 14:06:23 +0200 Subject: ubus: support obtaining numeric error code Some ubus users require access to the original ubus error status returned by various operations for fine grained error handling. Extend the error() function with an optional boolean argument which causes the function to return the numeric error code instead of a preformatted message when invoked. Signed-off-by: Jo-Philipp Wich --- lib/ubus.c | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) (limited to 'lib/ubus.c') diff --git a/lib/ubus.c b/lib/ubus.c index a9bafcb..1898e59 100644 --- a/lib/ubus.c +++ b/lib/ubus.c @@ -176,29 +176,37 @@ typedef struct { static uc_value_t * uc_ubus_error(uc_vm_t *vm, size_t nargs) { + uc_value_t *numeric = uc_fn_arg(0), *rv; uc_stringbuf_t *buf; const char *s; if (last_error.code == 0) return NULL; - buf = ucv_stringbuf_new(); - - if (last_error.code == UBUS_STATUS_UNKNOWN_ERROR && last_error.msg) { - ucv_stringbuf_addstr(buf, last_error.msg, strlen(last_error.msg)); + if (ucv_is_truish(numeric)) { + rv = ucv_int64_new(last_error.code); } else { - s = ubus_strerror(last_error.code); + buf = ucv_stringbuf_new(); + + if (last_error.code == UBUS_STATUS_UNKNOWN_ERROR && last_error.msg) { + ucv_stringbuf_addstr(buf, last_error.msg, strlen(last_error.msg)); + } + else { + s = ubus_strerror(last_error.code); - ucv_stringbuf_addstr(buf, s, strlen(s)); + ucv_stringbuf_addstr(buf, s, strlen(s)); - if (last_error.msg) - ucv_stringbuf_printf(buf, ": %s", last_error.msg); + if (last_error.msg) + ucv_stringbuf_printf(buf, ": %s", last_error.msg); + } + + rv = ucv_stringbuf_finish(buf); } set_error(0, NULL); - return ucv_stringbuf_finish(buf); + return rv; } static void -- cgit v1.2.3