@@ -261,14 +261,21 @@ nc_err(const struct ly_ctx *ctx, NC_ERR tag, ...)
261261{
262262 va_list ap ;
263263 struct lyd_node * err = NULL ;
264+ const struct lys_module * nc_mod ;
264265 NC_ERR_TYPE type ;
265266 const char * arg1 , * arg2 ;
266267 uint32_t sid ;
267268
268- NC_CHECK_ARG_RET (NULL , tag , NULL );
269+ NC_CHECK_ARG_RET (NULL , tag , ctx , NULL );
270+
271+ nc_mod = ly_ctx_get_module_implemented (ctx , "ietf-netconf" );
272+ if (!nc_mod ) {
273+ ERR (NULL , "Module \"ietf-netconf\" missing in the context." );
274+ return NULL ;
275+ }
269276
270277 /* rpc-error */
271- if (lyd_new_opaq2 (NULL , ctx , "rpc-error" , NULL , NULL , NC_NS_BASE , & err )) {
278+ if (lyd_new_inner (NULL , nc_mod , "rpc-error" , 0 , & err )) {
272279 return NULL ;
273280 }
274281
@@ -337,17 +344,17 @@ nc_err(const struct ly_ctx *ctx, NC_ERR tag, ...)
337344 ERRARG (NULL , "tag" );
338345 goto fail ;
339346 }
340- if (lyd_new_opaq2 (err , NULL , "error-type" , nc_err_type2str (type ), NULL , NC_NS_BASE , NULL )) {
347+ if (lyd_new_term (err , NULL , "error-type" , nc_err_type2str (type ), 0 , NULL )) {
341348 goto fail ;
342349 }
343350
344351 /* error-tag */
345- if (lyd_new_opaq2 (err , NULL , "error-tag" , nc_err_tag2str (tag ), NULL , NC_NS_BASE , NULL )) {
352+ if (lyd_new_term (err , NULL , "error-tag" , nc_err_tag2str (tag ), 0 , NULL )) {
346353 goto fail ;
347354 }
348355
349356 /* error-severity */
350- if (lyd_new_opaq2 (err , NULL , "error-severity" , "error" , NULL , NC_NS_BASE , NULL )) {
357+ if (lyd_new_term (err , NULL , "error-severity" , "error" , 0 , NULL )) {
351358 goto fail ;
352359 }
353360
@@ -474,13 +481,17 @@ nc_err(const struct ly_ctx *ctx, NC_ERR tag, ...)
474481API NC_ERR_TYPE
475482nc_err_get_type (const struct lyd_node * err )
476483{
477- struct lyd_node * match ;
484+ const struct lysc_node * schema ;
485+ struct lyd_node * match = NULL ;
478486
479487 NC_CHECK_ARG_RET (NULL , err , 0 );
480488
481- lyd_find_sibling_opaq_next (lyd_child (err ), "error-type" , & match );
489+ schema = lys_find_path (NULL , err -> schema , "error-type" , 0 );
490+ if (schema ) {
491+ lyd_find_sibling_val (lyd_child (err ), schema , NULL , 0 , & match );
492+ }
482493 if (match ) {
483- return nc_err_str2type ((( struct lyd_node_opaq * ) match )-> value );
494+ return nc_err_str2type (lyd_get_value ( match ));
484495 }
485496
486497 return 0 ;
@@ -489,13 +500,17 @@ nc_err_get_type(const struct lyd_node *err)
489500API NC_ERR
490501nc_err_get_tag (const struct lyd_node * err )
491502{
492- struct lyd_node * match ;
503+ const struct lysc_node * schema ;
504+ struct lyd_node * match = NULL ;
493505
494506 NC_CHECK_ARG_RET (NULL , err , 0 );
495507
496- lyd_find_sibling_opaq_next (lyd_child (err ), "error-tag" , & match );
508+ schema = lys_find_path (NULL , err -> schema , "error-tag" , 0 );
509+ if (schema ) {
510+ lyd_find_sibling_val (lyd_child (err ), schema , NULL , 0 , & match );
511+ }
497512 if (match ) {
498- return nc_err_str2tag ((( struct lyd_node_opaq * ) match )-> value );
513+ return nc_err_str2tag (lyd_get_value ( match ));
499514 }
500515
501516 return 0 ;
@@ -504,40 +519,45 @@ nc_err_get_tag(const struct lyd_node *err)
504519API int
505520nc_err_set_app_tag (struct lyd_node * err , const char * error_app_tag )
506521{
507- struct lyd_node * match , * prev_anchor ;
522+ const struct lysc_node * schema ;
523+ struct lyd_node * match ;
508524
509525 NC_CHECK_ARG_RET (NULL , err , error_app_tag , -1 );
510526
527+ /* find the schema node */
528+ schema = lys_find_path (NULL , err -> schema , "error-app-tag" , 0 );
529+ if (!schema ) {
530+ return -1 ;
531+ }
532+
511533 /* remove previous node */
512- lyd_find_sibling_opaq_next (lyd_child (err ), "error-app-tag" , & match );
534+ lyd_find_sibling_val (lyd_child (err ), schema , NULL , 0 , & match );
513535 if (match ) {
514536 lyd_free_tree (match );
515537 }
516538
517- /* find the previous node anchor */
518- lyd_find_sibling_opaq_next (lyd_child (err ), "error-severity" , & prev_anchor );
519-
520- /* create the node at the right place */
521- if (lyd_new_opaq2 (err , NULL , "error-app-tag" , error_app_tag , NULL , NC_NS_BASE , & match )) {
539+ /* create the node */
540+ if (lyd_new_term (err , NULL , "error-app-tag" , error_app_tag , 0 , & match )) {
522541 return -1 ;
523542 }
524- if (prev_anchor ) {
525- lyd_insert_after (prev_anchor , match );
526- }
527543
528544 return 0 ;
529545}
530546
531547API const char *
532548nc_err_get_app_tag (const struct lyd_node * err )
533549{
534- struct lyd_node * match ;
550+ const struct lysc_node * schema ;
551+ struct lyd_node * match = NULL ;
535552
536553 NC_CHECK_ARG_RET (NULL , err , NULL );
537554
538- lyd_find_sibling_opaq_next (lyd_child (err ), "error-app-tag" , & match );
555+ schema = lys_find_path (NULL , err -> schema , "error-tag" , 0 );
556+ if (schema ) {
557+ lyd_find_sibling_val (lyd_child (err ), schema , NULL , 0 , & match );
558+ }
539559 if (match ) {
540- return (( struct lyd_node_opaq * ) match )-> value ;
560+ return lyd_get_value ( match );
541561 }
542562
543563 return NULL ;
@@ -546,43 +566,45 @@ nc_err_get_app_tag(const struct lyd_node *err)
546566API int
547567nc_err_set_path (struct lyd_node * err , const char * error_path )
548568{
549- struct lyd_node * match , * prev_anchor ;
569+ const struct lysc_node * schema ;
570+ struct lyd_node * match ;
550571
551572 NC_CHECK_ARG_RET (NULL , err , error_path , -1 );
552573
574+ /* find the schema node */
575+ schema = lys_find_path (NULL , err -> schema , "error-path" , 0 );
576+ if (!schema ) {
577+ return -1 ;
578+ }
579+
553580 /* remove previous node */
554- lyd_find_sibling_opaq_next (lyd_child (err ), "error-path" , & match );
581+ lyd_find_sibling_val (lyd_child (err ), schema , NULL , 0 , & match );
555582 if (match ) {
556583 lyd_free_tree (match );
557584 }
558585
559- /* find the previous node anchor */
560- lyd_find_sibling_opaq_next (lyd_child (err ), "error-app-tag" , & prev_anchor );
561- if (!prev_anchor ) {
562- lyd_find_sibling_opaq_next (lyd_child (err ), "error-severity" , & prev_anchor );
563- }
564-
565- /* create the node at the right place */
566- if (lyd_new_opaq2 (err , NULL , "error-path" , error_path , NULL , NC_NS_BASE , & match )) {
586+ /* create the node */
587+ if (lyd_new_term (err , NULL , "error-path" , error_path , 0 , & match )) {
567588 return -1 ;
568589 }
569- if (prev_anchor ) {
570- lyd_insert_after (prev_anchor , match );
571- }
572590
573591 return 0 ;
574592}
575593
576594API const char *
577595nc_err_get_path (const struct lyd_node * err )
578596{
579- struct lyd_node * match ;
597+ const struct lysc_node * schema ;
598+ struct lyd_node * match = NULL ;
580599
581600 NC_CHECK_ARG_RET (NULL , err , NULL );
582601
583- lyd_find_sibling_opaq_next (lyd_child (err ), "error-path" , & match );
602+ schema = lys_find_path (NULL , err -> schema , "error-path" , 0 );
603+ if (schema ) {
604+ lyd_find_sibling_val (lyd_child (err ), schema , NULL , 0 , & match );
605+ }
584606 if (match ) {
585- return (( struct lyd_node_opaq * ) match )-> value ;
607+ return lyd_get_value ( match );
586608 }
587609
588610 return NULL ;
@@ -602,13 +624,13 @@ nc_err_set_msg(struct lyd_node *err, const char *error_message, const char *lang
602624 lyd_free_tree (match );
603625 }
604626
605- /* find the previous node anchor */
606- lyd_find_sibling_opaq_next ( lyd_child (err ), "error-path" , & prev_anchor );
607- if (! prev_anchor ) {
608- lyd_find_sibling_opaq_next ( lyd_child ( err ), "error-app-tag" , & prev_anchor ) ;
627+ /* find the previous node anchor (last non-opaque node) */
628+ prev_anchor = lyd_child (err );
629+ while ( prev_anchor && prev_anchor -> next && prev_anchor -> next -> schema ) {
630+ prev_anchor = prev_anchor -> next ;
609631 }
610- if (!prev_anchor ) {
611- lyd_find_sibling_opaq_next ( lyd_child ( err ), "error-severity" , & prev_anchor ) ;
632+ if (!prev_anchor -> schema ) {
633+ prev_anchor = NULL ;
612634 }
613635
614636 /* create the node at the right place */
@@ -617,6 +639,9 @@ nc_err_set_msg(struct lyd_node *err, const char *error_message, const char *lang
617639 }
618640 if (prev_anchor ) {
619641 lyd_insert_after (prev_anchor , match );
642+ } else if (match -> prev != match ) {
643+ /* some opaque nodes existed, this must be the first */
644+ lyd_insert_before (lyd_child (err ), match );
620645 }
621646
622647 if (lang && lyd_new_attr (match , NULL , "xml:lang" , lang , & attr )) {
0 commit comments