Remove RestconfDocumentedException.throwIf()
[netconf.git] / restconf / restconf-nb / src / main / java / org / opendaylight / restconf / nb / rfc8040 / databind / AbstractBody.java
index cb9d0d06034a65d460992b958ce6b13c8f121cc7..5c73ccea178d706f54d94b40df68b2a1889f745e 100644 (file)
@@ -15,6 +15,10 @@ import java.lang.invoke.MethodHandles;
 import java.lang.invoke.VarHandle;
 import org.eclipse.jdt.annotation.NonNull;
 import org.eclipse.jdt.annotation.Nullable;
+import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
+import org.opendaylight.restconf.common.errors.RestconfError;
+import org.opendaylight.yangtools.yang.data.api.YangNetconfError;
+import org.opendaylight.yangtools.yang.data.api.YangNetconfErrorAware;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -62,6 +66,22 @@ public abstract sealed class AbstractBody implements AutoCloseable
         return is;
     }
 
+
+    /**
+     * Throw a {@link RestconfDocumentedException} if the specified exception has a {@link YangNetconfError} attachment.
+     *
+     * @param cause Proposed cause of a RestconfDocumentedException
+     */
+    static void throwIfYangError(final Exception cause) {
+        if (cause instanceof YangNetconfErrorAware infoAware) {
+            throw new RestconfDocumentedException(cause, infoAware.getNetconfErrors().stream()
+                .map(error -> new RestconfError(error.type(), error.tag(), error.message(), error.appTag(),
+                    // FIXME: pass down error info
+                    null, error.path()))
+                .toList());
+        }
+    }
+
     private @Nullable InputStream getStream() {
         return (InputStream) INPUT_STREAM.getAndSet(this, null);
     }