From 86e251f30bcb7ca2f88676b8e885c2fe04e4b623 Mon Sep 17 00:00:00 2001 From: Olof hagsand Date: Wed, 26 Feb 2020 21:33:31 +0100 Subject: [PATCH] * Fixed: Some restconf errors were wrongly formatted such as: `{"ietf-restconf:errors":{"error":{"rpc-error":` . There should be no `"rpc-error"` level. --- CHANGELOG.md | 1 + apps/restconf/restconf_lib.c | 37 ++++++++++++++++++++++------ apps/restconf/restconf_methods_get.c | 6 +++-- 3 files changed, 35 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3f4f14a0..bb548d31 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -73,6 +73,7 @@ Expected: February 2020 ### Corrected Bugs +* Fixed: Some restconf errors were wrongly formatted such as: `{"ietf-restconf:errors":{"error":{"rpc-error":` . There should be no `"rpc-error"` level. * Fixed: Enabling modstate (CLICON_XMLDB_MODSTATE), changing a revision on a yang, and restarting made the backend daemon exit at start (thanks Matt) * Also: ensure to load `ietf-yang-library.yang ` if CLICON_XMLDB_MODSTATE is set * Fixed: Pretty-printed XML using prefixes not parsed correctly. diff --git a/apps/restconf/restconf_lib.c b/apps/restconf/restconf_lib.c index c0ce4202..9e1d1913 100644 --- a/apps/restconf/restconf_lib.c +++ b/apps/restconf/restconf_lib.c @@ -468,18 +468,41 @@ api_return_err(clicon_handle h, { int retval = -1; cbuf *cb = NULL; + cbuf *cberr = NULL; cxobj *xtag; char *tagstr; int code; + cxobj *xerr2 = NULL; const char *reason_phrase; clicon_debug(1, "%s", __FUNCTION__); - if ((cb = cbuf_new()) == NULL) + if ((cb = cbuf_new()) == NULL){ + clicon_err(OE_UNIX, errno, "cbuf_new"); goto done; - if ((xtag = xpath_first(xerr, NULL, "//error-tag")) == NULL){ - restconf_notfound(r); - goto ok; } + /* A well-formed error message when entering here should look like: + * ...invalid-value + * Check this is so, otherwise generate an internal error. + */ + if (strcmp(xml_name(xerr), "rpc-error") != 0 || + (xtag = xpath_first(xerr, NULL, "error-tag")) == NULL){ + if ((cberr = cbuf_new()) == NULL){ + clicon_err(OE_UNIX, errno, "cbuf_new"); + goto done; + } + cprintf(cberr, "Internal error, system returned invalid error message: "); + if (netconf_err2cb(xerr, cberr) < 0) + goto done; + if (netconf_operation_failed_xml(&xerr2, "application", + cbuf_get(cberr)) < 0) + goto done; + if ((xerr = xpath_first(xerr2, NULL, "//rpc-error")) == NULL){ + clicon_err(OE_XML, 0, "Internal error, shouldnt happen"); + goto done; + } + } + if (xml_name_set(xerr, "error") < 0) + goto done; tagstr = xml_body(xtag); if (code0 != 0) code = code0; @@ -489,8 +512,6 @@ api_return_err(clicon_handle h, } if ((reason_phrase = restconf_code2reason(code)) == NULL) reason_phrase=""; - if (xml_name_set(xerr, "error") < 0) - goto done; FCGX_SetExitStatus(code, r->out); /* Created */ FCGX_FPrintF(r->out, "Status: %d %s\r\n", code, reason_phrase); FCGX_FPrintF(r->out, "Content-Type: %s\r\n\r\n", restconf_media_int2str(media)); @@ -532,12 +553,14 @@ api_return_err(clicon_handle h, goto done; break; } /* switch media */ - ok: + // ok: retval = 0; done: clicon_debug(1, "%s retval:%d", __FUNCTION__, retval); if (cb) cbuf_free(cb); + if (cberr) + cbuf_free(cberr); return retval; } diff --git a/apps/restconf/restconf_methods_get.c b/apps/restconf/restconf_methods_get.c index 5f4c4eb0..d2bc9556 100644 --- a/apps/restconf/restconf_methods_get.c +++ b/apps/restconf/restconf_methods_get.c @@ -263,8 +263,10 @@ api_data_get2(clicon_handle h, if (netconf_invalid_value_xml(&xerr, "application", "Instance does not exist") < 0) goto done; /* override invalid-value default 400 with 404 */ - if (api_return_err(h, r, xerr, pretty, media_out, 404) < 0) - goto done; + if ((xe = xpath_first(xerr, NULL, "rpc-error")) != NULL){ + if (api_return_err(h, r, xe, pretty, media_out, 404) < 0) + goto done; + } goto ok; } switch (media_out){