Bump upstreams
[netconf.git] / restconf / restconf-nb / src / main / java / org / opendaylight / restconf / nb / rfc8040 / databind / AbstractBody.java
index c8846d20dee837ac6c69800ca6c18ec5c5f2ec50..8731c1c7748adb827146394a9ffb11817d5664e8 100644 (file)
@@ -9,19 +9,25 @@ package org.opendaylight.restconf.nb.rfc8040.databind;
 
 import static java.util.Objects.requireNonNull;
 
+import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
 import java.io.IOException;
 import java.io.InputStream;
 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;
 
 /**
  * An abstract request body backed by an {@link InputStream}.
  */
-public abstract sealed class AbstractBody implements AutoCloseable permits PatchBody, OperationInputBody {
+public abstract sealed class AbstractBody implements AutoCloseable
+        permits ChildBody, DataPostBody, OperationInputBody, PatchBody, ResourceBody {
     private static final Logger LOG = LoggerFactory.getLogger(AbstractBody.class);
 
     private static final VarHandle INPUT_STREAM;
@@ -35,6 +41,7 @@ public abstract sealed class AbstractBody implements AutoCloseable permits Patch
     }
 
     @SuppressWarnings("unused")
+    @SuppressFBWarnings(value = "URF_UNREAD_FIELD", justification = "https://github.com/spotbugs/spotbugs/issues/2749")
     private volatile InputStream inputStream;
 
     AbstractBody(final InputStream inputStream) {
@@ -61,6 +68,22 @@ public abstract sealed class AbstractBody implements AutoCloseable permits Patch
         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);
     }