Use ServerException in PatchBody.toPatchContext()
[netconf.git] / restconf / restconf-nb / src / main / java / org / opendaylight / restconf / server / api / JsonPatchBody.java
index 3891fe7bbf160addd9e411f7a34772561ab32a5c..5d1116a2da494afd1e04dba453b37bfabb4208fe 100644 (file)
@@ -23,7 +23,6 @@ import java.io.StringReader;
 import java.nio.charset.StandardCharsets;
 import java.util.concurrent.atomic.AtomicReference;
 import org.eclipse.jdt.annotation.NonNull;
-import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
 import org.opendaylight.restconf.common.patch.PatchContext;
 import org.opendaylight.restconf.common.patch.PatchEntity;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.patch.rev170222.yang.patch.yang.patch.Edit.Operation;
@@ -45,7 +44,8 @@ public final class JsonPatchBody extends PatchBody {
     }
 
     @Override
-    PatchContext toPatchContext(final ResourceContext resource, final InputStream inputStream) throws IOException {
+    PatchContext toPatchContext(final ResourceContext resource, final InputStream inputStream)
+            throws IOException, ServerException {
         try (var jsonReader = new JsonReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8))) {
             final var patchId = new AtomicReference<String>();
             final var resultList = read(jsonReader, resource, patchId);
@@ -55,7 +55,7 @@ public final class JsonPatchBody extends PatchBody {
     }
 
     private static ImmutableList<PatchEntity> read(final JsonReader in, final @NonNull ResourceContext resource,
-            final AtomicReference<String> patchId) throws IOException {
+            final AtomicReference<String> patchId) throws IOException, ServerException {
         final var edits = ImmutableList.<PatchEntity>builder();
         final var edit = new PatchEdit();
 
@@ -82,7 +82,7 @@ public final class JsonPatchBody extends PatchBody {
     private static void parseByName(final @NonNull String name, final @NonNull PatchEdit edit,
             final @NonNull JsonReader in, final @NonNull ResourceContext resource,
             final @NonNull Builder<PatchEntity> resultCollection, final @NonNull AtomicReference<String> patchId)
-                throws IOException {
+                throws IOException, ServerException  {
         switch (name) {
             case "edit" -> {
                 if (in.peek() == JsonToken.BEGIN_ARRAY) {
@@ -110,7 +110,7 @@ public final class JsonPatchBody extends PatchBody {
 
     // Read one patch edit object from JSON input
     private static void readEditDefinition(final @NonNull PatchEdit edit, final @NonNull JsonReader in,
-            final @NonNull ResourceContext resource) throws IOException {
+            final @NonNull ResourceContext resource) throws IOException, ServerException {
         String deferredValue = null;
         in.beginObject();
 
@@ -269,8 +269,9 @@ public final class JsonPatchBody extends PatchBody {
      * Prepare PatchEntity from PatchEdit instance when it satisfies conditions, otherwise throws exception.
      * @param edit Instance of PatchEdit
      * @return PatchEntity Patch entity
+     * @throws ServerException if the {@link PatchEdit} is not consistent
      */
-    private static PatchEntity prepareEditOperation(final @NonNull PatchEdit edit) {
+    private static PatchEntity prepareEditOperation(final @NonNull PatchEdit edit) throws ServerException {
         if (edit.getOperation() != null && edit.getTargetSchemaNode() != null
             && checkDataPresence(edit.getOperation(), edit.getData() != null)) {
             if (!requiresValue(edit.getOperation())) {
@@ -288,7 +289,7 @@ public final class JsonPatchBody extends PatchBody {
             return new PatchEntity(edit.getId(), edit.getOperation(), targetNode, edit.getData());
         }
 
-        throw new RestconfDocumentedException("Error parsing input", ErrorType.PROTOCOL, ErrorTag.MALFORMED_MESSAGE);
+        throw new ServerException(ErrorType.PROTOCOL, ErrorTag.MALFORMED_MESSAGE, "Error parsing input");
     }
 
     /**