changed netconf_err2cb

This commit is contained in:
Olof hagsand 2019-11-12 20:47:09 +01:00
parent 70caeaa783
commit 6b9a9d46f9
5 changed files with 29 additions and 31 deletions

View file

@ -115,6 +115,7 @@ clixon_plugin_statedata(clicon_handle h,
cxobj *x = NULL; cxobj *x = NULL;
clixon_plugin *cp = NULL; clixon_plugin *cp = NULL;
plgstatedata_t *fn; /* Plugin statedata fn */ plgstatedata_t *fn; /* Plugin statedata fn */
cbuf *cberr = NULL;
while ((cp = clixon_plugin_each(h, cp)) != NULL) { while ((cp = clixon_plugin_each(h, cp)) != NULL) {
if ((fn = cp->cp_api.ca_statedata) == NULL) if ((fn = cp->cp_api.ca_statedata) == NULL)
@ -133,21 +134,11 @@ clixon_plugin_statedata(clicon_handle h,
if (ret > 0 && (ret = xml_yang_validate_add(h, x, &xerr)) < 0) if (ret > 0 && (ret = xml_yang_validate_add(h, x, &xerr)) < 0)
goto done; goto done;
if (ret == 0){ if (ret == 0){
cbuf *cberr = NULL; /* XXX Cumbersome, try to fold into one cb */ cprintf(cberr, "Internal error: state callback returned invalid XML: ");
cbuf *cberr2 = NULL; if (netconf_err2cb(xpath_first(xerr, "rpc-error"), cberr) < 0)
if (netconf_err2cb(xpath_first(xerr, "rpc-error"), &cberr) < 0)
goto done; goto done;
if ((cberr2 = cbuf_new()) == NULL){ if (netconf_operation_failed_xml(xret, "application", cbuf_get(cberr))< 0)
clicon_err(OE_UNIX, errno, "cbuf_new");
goto done; goto done;
}
cprintf(cberr2, "Internal error: state callback returned invalid XML: %s", cbuf_get(cberr));
if (netconf_operation_failed_xml(xret, "application", cbuf_get(cberr2))< 0)
goto done;
if (cberr)
cbuf_free(cberr);
if (cberr2)
cbuf_free(cberr2);
goto fail; goto fail;
} }
if ((ret = netconf_trymerge(x, yspec, xret)) < 0) if ((ret = netconf_trymerge(x, yspec, xret)) < 0)
@ -161,6 +152,8 @@ clixon_plugin_statedata(clicon_handle h,
} }
retval = 1; retval = 1;
done: done:
if (cberr)
cbuf_free(cberr);
if (x) if (x)
xml_free(x); xml_free(x);
if (xerr) if (xerr)

View file

@ -90,7 +90,7 @@ int netconf_trymerge(cxobj *x, yang_stmt *yspec, cxobj **xret);
int netconf_module_features(clicon_handle h); int netconf_module_features(clicon_handle h);
int netconf_module_load(clicon_handle h); int netconf_module_load(clicon_handle h);
char *netconf_db_find(cxobj *xn, char *name); char *netconf_db_find(cxobj *xn, char *name);
int netconf_err2cb(cxobj *xerr, cbuf **cberr); int netconf_err2cb(cxobj *xerr, cbuf *cberr);
const netconf_content netconf_content_str2int(char *str); const netconf_content netconf_content_str2int(char *str);
const char *netconf_content_int2str(netconf_content nr); const char *netconf_content_int2str(netconf_content nr);
int netconf_hello_server(clicon_handle h, cbuf *cb, uint32_t session_id); int netconf_hello_server(clicon_handle h, cbuf *cb, uint32_t session_id);

View file

@ -1339,38 +1339,35 @@ netconf_db_find(cxobj *xn,
/*! Generate netconf error msg to cbuf to use in string printout or logs /*! Generate netconf error msg to cbuf to use in string printout or logs
* @param[in] xerr Netconf error message on the level: <rpc-error> * @param[in] xerr Netconf error message on the level: <rpc-error>
* @param[out] cberr Translation from netconf err to cbuf. Free with cbuf_free. * @param[in,out] cberr Translation from netconf err to cbuf.
* @retval 0 OK, with cberr set * @retval 0 OK, with cberr set
* @retval -1 Error * @retval -1 Error
* @code * @code
* cbuf *cb = NULL; * cbuf *cb = NULL;
* if (netconf_err2cb(xerr, &cb) < 0) * if ((cb = cbuf_new()) ==NULL){
* err;
* if (netconf_err2cb(xerr, cb) < 0)
* err; * err;
* printf("%s", cbuf_get(cb)); * printf("%s", cbuf_get(cb));
* cbuf_free(cb);
* @endcode * @endcode
* @see clicon_rpc_generate_error * @see clicon_rpc_generate_error
*/ */
int int
netconf_err2cb(cxobj *xerr, netconf_err2cb(cxobj *xerr,
cbuf **cberr) cbuf *cberr)
{ {
int retval = -1; int retval = -1;
cbuf *cb = NULL;
cxobj *x; cxobj *x;
if ((cb = cbuf_new()) ==NULL){
clicon_err(OE_XML, errno, "cbuf_new");
goto done;
}
if ((x=xpath_first(xerr, "error-type"))!=NULL) if ((x=xpath_first(xerr, "error-type"))!=NULL)
cprintf(cb, "%s ", xml_body(x)); cprintf(cberr, "%s ", xml_body(x));
if ((x=xpath_first(xerr, "error-tag"))!=NULL) if ((x=xpath_first(xerr, "error-tag"))!=NULL)
cprintf(cb, "%s ", xml_body(x)); cprintf(cberr, "%s ", xml_body(x));
if ((x=xpath_first(xerr, "error-message"))!=NULL) if ((x=xpath_first(xerr, "error-message"))!=NULL)
cprintf(cb, "%s ", xml_body(x)); cprintf(cberr, "%s ", xml_body(x));
if ((x=xpath_first(xerr, "error-info"))!=NULL) if ((x=xpath_first(xerr, "error-info"))!=NULL)
clicon_xml2cbuf(cb, xml_child_i(x,0), 0, 0, -1); clicon_xml2cbuf(cberr, xml_child_i(x,0), 0, 0, -1);
*cberr = cb;
retval = 0; retval = 0;
done: done:
return retval; return retval;

View file

@ -236,7 +236,11 @@ clicon_rpc_generate_error(const char *prefix,
int retval = -1; int retval = -1;
cbuf *cb = NULL; cbuf *cb = NULL;
if (netconf_err2cb(xerr, &cb) < 0) if ((cb = cbuf_new()) ==NULL){
clicon_err(OE_XML, errno, "cbuf_new");
goto done;
}
if (netconf_err2cb(xerr, cb) < 0)
goto done; goto done;
if (prefix) if (prefix)
clicon_log(LOG_ERR, "%s: %s", prefix, cbuf_get(cb)); clicon_log(LOG_ERR, "%s: %s", prefix, cbuf_get(cb));

View file

@ -234,7 +234,11 @@ main(int argc,
if (ret > 0 && (ret = xml_yang_validate_add(h, xc, &xerr)) < 0) if (ret > 0 && (ret = xml_yang_validate_add(h, xc, &xerr)) < 0)
goto done; goto done;
if (ret == 0){ if (ret == 0){
if (netconf_err2cb(xerr, &cbret) < 0) if ((cbret = cbuf_new()) ==NULL){
clicon_err(OE_XML, errno, "cbuf_new");
goto done;
}
if (netconf_err2cb(xerr, cbret) < 0)
goto done; goto done;
fprintf(stderr, "xml validation error: %s\n", cbuf_get(cbret)); fprintf(stderr, "xml validation error: %s\n", cbuf_get(cbret));
goto done; goto done;