From: Robert Varga Date: Tue, 23 Feb 2021 18:35:59 +0000 (+0100) Subject: Update OperationFailedException design X-Git-Tag: v7.0.0~85 X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=commitdiff_plain;h=5efd2c346280e705c7bc14d8fa503dc7c68b0089;p=yangtools.git Update OperationFailedException design 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 --- diff --git a/yang/yang-common/src/main/java/module-info.java b/yang/yang-common/src/main/java/module-info.java index 7134b39f92..6caf41dd19 100644 --- a/yang/yang-common/src/main/java/module-info.java +++ b/yang/yang-common/src/main/java/module-info.java @@ -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 diff --git a/yang/yang-common/src/main/java/org/opendaylight/yangtools/yang/common/OperationFailedException.java b/yang/yang-common/src/main/java/org/opendaylight/yangtools/yang/common/OperationFailedException.java index 190b5a9303..dc47053264 100644 --- a/yang/yang-common/src/main/java/org/opendaylight/yangtools/yang/common/OperationFailedException.java +++ b/yang/yang-common/src/main/java/org/opendaylight/yangtools/yang/common/OperationFailedException.java @@ -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 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 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 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 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()); } }