]> git.puffer.fish Git - mirror/frr.git/commitdiff
lib: mgmtd: cleanup error value for native messaging
authorChristian Hopps <chopps@labn.net>
Tue, 17 Sep 2024 06:27:03 +0000 (02:27 -0400)
committerChristian Hopps <chopps@labn.net>
Tue, 17 Sep 2024 07:04:59 +0000 (03:04 -0400)
- Now if positive it's libyang LY_ERR, otherwise it's `-errno` value.

Signed-off-by: Christian Hopps <chopps@labn.net>
lib/northbound.c
lib/northbound.h
mgmtd/mgmt_fe_adapter.h
mgmtd/mgmt_txn.c
mgmtd/mgmt_txn.h

index 63062ee3369ec2526291e06387fd3e0041d24445..35d0596ed4e309488e0060656099a3d9dd981831 100644 (file)
@@ -908,7 +908,7 @@ static int nb_candidate_edit_tree_add(struct nb_config *candidate,
                        if (operation == NB_OP_CREATE_EXCL) {
                                snprintf(errmsg, errmsg_len,
                                         "Data already exists");
-                               ret = NB_ERR;
+                               ret = NB_ERR_EXISTS;
                                goto done;
                        }
 
@@ -995,7 +995,7 @@ static int nb_candidate_edit_tree_del(struct nb_config *candidate,
        if (!dnode || (dnode->flags & LYD_DEFAULT)) {
                if (operation == NB_OP_DELETE) {
                        snprintf(errmsg, errmsg_len, "Data missing");
-                       return NB_ERR;
+                       return NB_ERR_NOT_FOUND;
                } else
                        return NB_OK;
        }
@@ -2605,6 +2605,8 @@ const char *nb_err_name(enum nb_error error)
                return "no changes";
        case NB_ERR_NOT_FOUND:
                return "element not found";
+       case NB_ERR_EXISTS:
+               return "element already exists";
        case NB_ERR_LOCKED:
                return "resource is locked";
        case NB_ERR_VALIDATION:
index da5f5be1ee9fdbf2447eef97ceaa50b06507d45b..b311affa31f8700b3b7601efeebd9550ca5e6219 100644 (file)
@@ -678,6 +678,7 @@ enum nb_error {
        NB_ERR,
        NB_ERR_NO_CHANGES,
        NB_ERR_NOT_FOUND,
+       NB_ERR_EXISTS,
        NB_ERR_LOCKED,
        NB_ERR_VALIDATION,
        NB_ERR_RESOURCE,
index 61d6cfae13aa399f0e3fcdc03cca7095e84901b5..5a7dec3e6ff765bf8b995dc3a9c1e0dffa4b5c8d 100644 (file)
@@ -194,7 +194,7 @@ extern int mgmt_fe_adapter_send_rpc_reply(uint64_t session_id, uint64_t txn_id,
  *     unlock: implicit-lock flag was set in the request
  *     commit: implicit-commit flag was set in the request
  *     xpath: the xpath of the data node that was created
- *     error: the error code, zero for successful request
+ *     error: >0 LY_ERR, < 0 -errno
  *     errstr: the error string, if error is non-zero
  */
 extern int mgmt_fe_adapter_send_edit_reply(uint64_t session_id, uint64_t txn_id,
@@ -210,7 +210,7 @@ extern int mgmt_fe_adapter_send_edit_reply(uint64_t session_id, uint64_t txn_id,
  * Args:
  *     txn_id: the txn_id this error pertains to.
  *     short_circuit_ok: True if OK to short-circuit the call.
- *     error: An integer error value.
+ *      error: >0 LY_ERR, < 0 -errno
  *     errfmt: An error format string (i.e., printfrr)
  *      ...: args for use by the `errfmt` format string.
  *
index ed133243a1a3b8a33fc3274c60b61e9b785d50b9..53d9f5c3fabf38cb90d76f0942ed4acb0672cb57 100644 (file)
@@ -1335,7 +1335,8 @@ static int txn_get_tree_data_done(struct mgmt_txn_ctx *txn,
                          " req_id %" PRIu64 " to requested type %u",
                          txn->txn_id, req_id, get_tree->result_type);
 
-               (void)mgmt_fe_adapter_txn_error(txn->txn_id, req_id, false, ret,
+               (void)mgmt_fe_adapter_txn_error(txn->txn_id, req_id, false,
+                                               errno_from_nb_error(ret),
                                                "Error converting results of GETTREE");
        }
 
@@ -1351,7 +1352,7 @@ static int txn_rpc_done(struct mgmt_txn_ctx *txn, struct mgmt_txn_req *txn_req)
        EVENT_OFF(txn->rpc_timeout);
 
        if (rpc->errstr)
-               mgmt_fe_adapter_txn_error(txn->txn_id, req_id, false, -1,
+               mgmt_fe_adapter_txn_error(txn->txn_id, req_id, false, -EINVAL,
                                          rpc->errstr);
        else if (mgmt_fe_adapter_send_rpc_reply(txn->session_id, txn->txn_id,
                                                req_id, rpc->result_type,
@@ -1360,7 +1361,8 @@ static int txn_rpc_done(struct mgmt_txn_ctx *txn, struct mgmt_txn_req *txn_req)
                          " req_id %" PRIu64 " to requested type %u",
                          txn->txn_id, req_id, rpc->result_type);
 
-               (void)mgmt_fe_adapter_txn_error(txn->txn_id, req_id, false, -1,
+               (void)mgmt_fe_adapter_txn_error(txn->txn_id, req_id, false,
+                                               -EINVAL,
                                                "Error converting results of RPC");
        }
 
@@ -2580,7 +2582,7 @@ int mgmt_txn_send_edit(uint64_t txn_id, uint64_t req_id,
 reply:
        mgmt_fe_adapter_send_edit_reply(txn->session_id, txn->txn_id, req_id,
                                        unlock, commit, edit->xpath_created,
-                                       ret ? -1 : 0, errstr);
+                                       errno_from_nb_error(ret), errstr);
 
        XFREE(MTYPE_MGMTD_TXN_REQ, edit);
 
index b6ca288675b75e0ed88ec932594804765e74aa38..37dadc01714aa6bbe19b657041c12b2cba29cae7 100644 (file)
@@ -69,6 +69,34 @@ static inline const char *mgmt_txn_type2str(enum mgmt_txn_type type)
        return "Unknown";
 }
 
+
+static inline int16_t errno_from_nb_error(enum nb_error ret)
+{
+       switch (ret) {
+       case NB_OK:
+               return 0;
+       case NB_ERR_NO_CHANGES:
+               return -EALREADY;
+       case NB_ERR_NOT_FOUND:
+               return -ENOENT;
+       case NB_ERR_EXISTS:
+               return -EEXIST;
+       case NB_ERR_LOCKED:
+               return -EWOULDBLOCK;
+       case NB_ERR_VALIDATION:
+               return -EINVAL;
+       case NB_ERR_RESOURCE:
+               return -ENOMEM;
+       case NB_ERR:
+       case NB_ERR_INCONSISTENCY:
+               return -EINVAL;
+       case NB_YIELD:
+       default:
+               return -EINVAL;
+       }
+}
+
+
 /* Initialise transaction module. */
 extern int mgmt_txn_init(struct mgmt_master *cm, struct event_loop *tm);