Update OperationFailedException design 29/95329/2
authorRobert Varga <robert.varga@pantheon.tech>
Tue, 23 Feb 2021 18:35:59 +0000 (19:35 +0100)
committerRobert Varga <nite@hq.sk>
Wed, 24 Feb 2021 09:28:10 +0000 (09:28 +0000)
The constructors are a pure hell to work with. Add Collection-based
errors as well as a single-error constructor. Improve defences
against nulls while retaining reasonable behavior.

Change-Id: I80e0866170ae78edb0bcdf526465125ba956f90e
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
yang/yang-common/src/main/java/module-info.java
yang/yang-common/src/main/java/org/opendaylight/yangtools/yang/common/OperationFailedException.java

index 7134b39f9243c856b200ccb3a3510a75e1bef0ee..6caf41dd1936a7e5a83805418d29fc27fffc67e5 100644 (file)
@@ -28,6 +28,7 @@ module org.opendaylight.yangtools.yang.common {
         Uint64.Support;
 
     requires transitive org.opendaylight.yangtools.concepts;
+    requires com.google.common;
     requires org.slf4j;
 
     // Annotations
index 190b5a9303d88ab48deef5a365c13bf4e950bb43..dc4705326447cbb49b66409c47e424f8323ccfa0 100644 (file)
@@ -5,14 +5,15 @@
  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
  * and is available at http://www.eclipse.org/legal/epl-v10.html
  */
-
 package org.opendaylight.yangtools.yang.common;
 
 import static java.util.Objects.requireNonNull;
 
 import com.google.common.base.MoreObjects;
+import com.google.common.base.MoreObjects.ToStringHelper;
 import com.google.common.collect.ImmutableList;
 import java.util.Arrays;
+import java.util.Collection;
 import java.util.List;
 import org.opendaylight.yangtools.yang.common.RpcError.ErrorType;
 
@@ -22,17 +23,66 @@ import org.opendaylight.yangtools.yang.common.RpcError.ErrorType;
  * @author Thomas Pantelis
  */
 public class OperationFailedException extends Exception {
-
     private static final long serialVersionUID = 1L;
 
     private final ImmutableList<RpcError> errorList;
 
+    /**
+     * Constructs a new instance with the specified detail message and cause.
+     *
+     * @param message the detail message
+     * @param cause the cause
+     * @throws NullPointerException if {@code message} is {@code null}
+     */
+    public OperationFailedException(final String message, final Throwable cause) {
+        super(requireNonNull(message), cause);
+        errorList = null;
+    }
+
+    /**
+     * Constructs a new instance with the specified detail message and error.
+     *
+     * @param message the detail message
+     * @param error {@link RpcError} instance that provides additional error information about this exception
+     * @throws NullPointerException if any argument is {@code null}
+     */
+    public OperationFailedException(final String message, final RpcError error) {
+        super(requireNonNull(message));
+        errorList = ImmutableList.of(error);
+    }
+
+    /**
+     * Constructs a new instance with the specified detail message, cause and errors.
+     *
+     * @param message the detail message
+     * @param cause the cause
+     * @param errors {@link RpcError} instances that provide additional error information about this exception
+     * @throws NullPointerException if either {@code message} or {@code errors} is {@code null}, or if {@code errors}
+     *                              contains a {@code null} element.
+     */
+    public OperationFailedException(final String message, final Throwable cause, final Collection<RpcError> errors) {
+        super(requireNonNull(message), cause);
+        errorList = errors.isEmpty() ? null : ImmutableList.copyOf(errors);
+    }
+
+    /**
+     * Constructs a new instance with the specified detail message and errors.
+     *
+     * @param message the detail message
+     * @param errors {@link RpcError} instances that provide additional error information about this exception
+     * @throws NullPointerException if any argument is, or {@code errors} contains, {@code null}
+     */
+    public OperationFailedException(final String message, final Collection<? extends RpcError> errors) {
+        super(requireNonNull(message));
+        this.errorList = ImmutableList.copyOf(errors);
+    }
+
     /**
      * Constructs a new instance with the specified detail message and errors.
      *
      * @param message the detail message
-     * @param errors {@link RpcError} instances that provide additional error information about
-     *               this exception
+     * @param errors {@link RpcError} instances that provide additional error information about this exception
+     * @throws NullPointerException if any argument is, or {@code errors} contains, {@code null}
      */
     public OperationFailedException(final String message, final RpcError... errors) {
         this(message, null, errors);
@@ -43,20 +93,10 @@ public class OperationFailedException extends Exception {
      *
      * @param message the detail message
      * @param cause the cause
-     * @param errors {@link RpcError} instances that provide additional error information about
-     *               this exception
+     * @param errors {@link RpcError} instances that provide additional error information about this exception
      */
-    public OperationFailedException(final String message, final Throwable cause,
-                                    final RpcError... errors) {
-        super(requireNonNull(message), cause);
-
-        if (errors != null && errors.length > 0) {
-            errorList = ImmutableList.copyOf(Arrays.asList(errors));
-        } else {
-            // Add a default RpcError.
-            errorList = ImmutableList.of(RpcResultBuilder.newError(ErrorType.APPLICATION, null,
-                    getMessage(), null, null, getCause()));
-        }
+    public OperationFailedException(final String message, final Throwable cause, final RpcError... errors) {
+        this(message, null, Arrays.asList(errors));
     }
 
     /**
@@ -65,12 +105,16 @@ public class OperationFailedException extends Exception {
      * @return a List of RpcErrors. There is always at least one RpcError.
      */
     public List<RpcError> getErrorList() {
-        return errorList;
+        return errorList != null ? errorList : ImmutableList.of(
+            RpcResultBuilder.newError(ErrorType.APPLICATION, null, getMessage(), null, null, getCause()));
     }
 
     @Override
-    public String toString() {
-        return MoreObjects.toStringHelper(this).add("message", getMessage())
-                .add("errorList", errorList).toString();
+    public final String toString() {
+        return addToStringAttributes(MoreObjects.toStringHelper(this)).toString();
+    }
+
+    protected ToStringHelper addToStringAttributes(final ToStringHelper helper) {
+        return helper.add("message", getMessage()).add("errorList", getErrorList());
     }
 }