Migrate ResourceBody to ServerException
[netconf.git] / restconf / restconf-nb / src / main / java / org / opendaylight / restconf / server / api / ResourceBody.java
index da143d4a2fc19fd6828dc839382f7862c2a121ed..964d013646b4d8ab97bd8776cc57641f6f6b4083 100644 (file)
@@ -10,7 +10,7 @@ package org.opendaylight.restconf.server.api;
 import java.io.IOException;
 import java.io.InputStream;
 import org.eclipse.jdt.annotation.NonNull;
-import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
+import org.eclipse.jdt.annotation.NonNullByDefault;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.restconf.rev170126.restconf.restconf.Data;
 import org.opendaylight.yangtools.yang.common.ErrorTag;
 import org.opendaylight.yangtools.yang.common.ErrorType;
@@ -40,10 +40,10 @@ public abstract sealed class ResourceBody extends RequestBody permits JsonResour
      * Acquire the {@link NormalizedNode} representation of this body.
      *
      * @param path A {@link DatabindPath.Data} corresponding to the body
-     * @throws RestconfDocumentedException if the body cannot be decoded or it does not match {@code path}
+     * @throws ServerException if the body cannot be decoded or it does not match {@code path}
      */
-    @SuppressWarnings("checkstyle:illegalCatch")
-    public final @NonNull NormalizedNode toNormalizedNode(final DatabindPath.@NonNull Data path) {
+    public final @NonNull NormalizedNode toNormalizedNode(final DatabindPath.@NonNull Data path)
+            throws ServerException {
         final var instance = path.instance();
         final var expectedName = instance.isEmpty() ? DATA_NID : instance.getLastPathArgument();
         final var holder = new NormalizationResultHolder();
@@ -51,14 +51,7 @@ public abstract sealed class ResourceBody extends RequestBody permits JsonResour
             streamTo(path, expectedName, consume(), streamWriter);
         } catch (IOException e) {
             LOG.debug("Error reading input", e);
-            throw new RestconfDocumentedException("Error parsing input: " + e.getMessage(), ErrorType.PROTOCOL,
-                    ErrorTag.MALFORMED_MESSAGE, e);
-        } catch (RestconfDocumentedException e) {
-            throw e;
-        } catch (RuntimeException e) {
-            throwIfYangError(e);
-            throw new RestconfDocumentedException("Error parsing input: " + e.getMessage(), ErrorType.PROTOCOL,
-                ErrorTag.MALFORMED_MESSAGE, e);
+            throw path.databind().newProtocolMalformedMessageServerException("Error parsing input", e);
         }
 
         final var parsedData = holder.getResult().data();
@@ -73,14 +66,14 @@ public abstract sealed class ResourceBody extends RequestBody permits JsonResour
 
         final var dataName = data.name();
         if (!dataName.equals(expectedName)) {
-            throw new RestconfDocumentedException(
-                "Payload name (" + dataName + ") is different from identifier name (" + expectedName + ")",
-                ErrorType.PROTOCOL, ErrorTag.MALFORMED_MESSAGE);
+            throw new ServerException(ErrorType.PROTOCOL, ErrorTag.MALFORMED_MESSAGE,
+                "Payload name (%s) is different from identifier name (%s)", dataName, expectedName);
         }
 
         return data;
     }
 
-    abstract void streamTo(DatabindPath.@NonNull Data path, @NonNull PathArgument name,
-        @NonNull InputStream inputStream, @NonNull NormalizedNodeStreamWriter writer) throws IOException;
+    @NonNullByDefault
+    abstract void streamTo(DatabindPath.Data path, PathArgument name, InputStream inputStream,
+        NormalizedNodeStreamWriter writer) throws ServerException;
 }
\ No newline at end of file