Clean up RestconfError 78/108578/3
authorRobert Varga <robert.varga@pantheon.tech>
Fri, 20 Oct 2023 17:42:25 +0000 (19:42 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Fri, 20 Oct 2023 17:45:33 +0000 (19:45 +0200)
Update documentation and clean up conversion bits from RpcError.

JIRA: NETCONF-1188
Change-Id: I446dd8e4c14473241838c95b5220aa7ec2253efd
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
restconf/restconf-common/src/main/java/org/opendaylight/restconf/common/errors/RestconfError.java

index fbf66610efe66c29619735557e5747b0b081cea5..36949fd5c94bae2915574bffb9c3da395ccbc134 100644 (file)
@@ -10,117 +10,97 @@ package org.opendaylight.restconf.common.errors;
 import static java.util.Objects.requireNonNull;
 
 import java.io.Serializable;
+import org.eclipse.jdt.annotation.NonNull;
 import org.opendaylight.yangtools.yang.common.ErrorTag;
 import org.opendaylight.yangtools.yang.common.ErrorType;
 import org.opendaylight.yangtools.yang.common.RpcError;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
 
 /**
- * Encapsulates a restconf error as defined in the ietf restconf draft.
- *
- * <br>
- * <br>
- * <b>Note:</b> Enumerations defined within are provided by the ietf restconf draft.
+ * Encapsulates a single {@code error} within the
+ * <a href="https://www.rfc-editor.org/rfc/rfc8040#section-3.9">"errors" YANG Data Template</a>.
  *
  * @author Devin Avery
- *     See also <a href="https://tools.ietf.org/html/draft-bierman-netconf-restconf-02">RESTCONF</a>.
  */
 public class RestconfError implements Serializable {
     @java.io.Serial
     private static final long serialVersionUID = 1L;
 
-    private final ErrorType errorType;
-    private final ErrorTag errorTag;
+    private final @NonNull ErrorType errorType;
+    private final @NonNull ErrorTag errorTag;
+    // FIXME: 'errorInfo' is defined as a formatted XML string. We need a better representation to enable reasonable
+    //        interop with JSON (and others)
     private final String errorInfo;
     private final String errorAppTag;
     private final String errorMessage;
     private final YangInstanceIdentifier errorPath;
 
     /**
-     * Constructs a RestConfError.
+     * Constructs a RestconfError.
      *
-     * @param errorType
-     *            The enumerated type indicating the layer where the error occurred.
-     * @param errorTag
-     *            The enumerated tag representing a more specific error cause.
-     * @param errorMessage
-     *            A string which provides a plain text string describing the error.
+     * @param errorType The enumerated type indicating the layer where the error occurred.
+     * @param errorTag The enumerated tag representing a more specific error cause.
+     * @param errorMessage A string which provides a plain text string describing the error.
      */
     public RestconfError(final ErrorType errorType, final ErrorTag errorTag, final String errorMessage) {
         this(errorType, errorTag, errorMessage, null, null, null);
     }
 
     /**
-     * Constructs a RestConfError object.
+     * Constructs a RestconfError object.
      *
-     * @param errorType
-     *            The enumerated type indicating the layer where the error occurred.
-     * @param errorTag
-     *            The enumerated tag representing a more specific error cause.
-     * @param errorMessage
-     *            A string which provides a plain text string describing the error.
-     * @param errorAppTag
-     *            A string which represents an application-specific error tag that further specifies the error cause.
+     * @param errorType The enumerated type indicating the layer where the error occurred.
+     * @param errorTag The enumerated tag representing a more specific error cause.
+     * @param errorMessage A string which provides a plain text string describing the error.
+     * @param errorAppTag A string which represents an application-specific error tag that further specifies the error
+     *                    cause.
      */
     public RestconfError(final ErrorType errorType, final ErrorTag errorTag, final String errorMessage,
-                         final String errorAppTag) {
+            final String errorAppTag) {
         this(errorType, errorTag, errorMessage, errorAppTag, null, null);
     }
 
     /**
-     * Constructs a RestConfError object.
+     * Constructs a RestconfError object.
      *
-     * @param errorType
-     *            The enumerated type indicating the layer where the error occurred.
-     * @param errorTag
-     *            The enumerated tag representing a more specific error cause.
-     * @param errorMessage
-     *            A string which provides a plain text string describing the error.
-     * @param errorPath
-     *            An instance identifier which contains error path
+     * @param errorType The enumerated type indicating the layer where the error occurred.
+     * @param errorTag The enumerated tag representing a more specific error cause.
+     * @param errorMessage A string which provides a plain text string describing the error.
+     * @param errorPath An instance identifier which contains error path
      */
     public RestconfError(final ErrorType errorType, final ErrorTag errorTag, final String errorMessage,
-                         final YangInstanceIdentifier errorPath) {
+            final YangInstanceIdentifier errorPath) {
         this(errorType, errorTag, errorMessage, null, null, errorPath);
     }
 
     /**
-     * Constructs a RestConfError object.
+     * Constructs a RestconfError object.
      *
-     * @param errorType
-     *            The enumerated type indicating the layer where the error occurred.
-     * @param errorTag
-     *            The enumerated tag representing a more specific error cause.
-     * @param errorMessage
-     *            A string which provides a plain text string describing the error.
-     * @param errorAppTag
-     *            A string which represents an application-specific error tag that further specifies the error cause.
-     * @param errorInfo
-     *            A string, <b>formatted as XML</b>, which contains additional error information.
+     * @param errorType The enumerated type indicating the layer where the error occurred.
+     * @param errorTag The enumerated tag representing a more specific error cause.
+     * @param errorMessage A string which provides a plain text string describing the error.
+     * @param errorAppTag A string which represents an application-specific error tag that further specifies the error
+     *                    cause.
+     * @param errorInfo A string, <b>formatted as XML</b>, which contains additional error information.
      */
     public RestconfError(final ErrorType errorType, final ErrorTag errorTag, final String errorMessage,
-                         final String errorAppTag, final String errorInfo) {
+            final String errorAppTag, final String errorInfo) {
         this(errorType, errorTag, errorMessage, errorAppTag, errorInfo, null);
     }
 
     /**
      * Constructs a RestConfError object.
      *
-     * @param errorType
-     *            The enumerated type indicating the layer where the error occurred.
-     * @param errorTag
-     *            The enumerated tag representing a more specific error cause.
-     * @param errorMessage
-     *            A string which provides a plain text string describing the error.
-     * @param errorAppTag
-     *            A string which represents an application-specific error tag that further specifies the error cause.
-     * @param errorInfo
-     *            A string, <b>formatted as XML</b>, which contains additional error information.
-     * @param errorPath
-     *            An instance identifier which contains error path
+     * @param errorType The enumerated type indicating the layer where the error occurred.
+     * @param errorTag The enumerated tag representing a more specific error cause.
+     * @param errorMessage A string which provides a plain text string describing the error.
+     * @param errorAppTag A string which represents an application-specific error tag that further specifies the error
+     *                    cause.
+     * @param errorInfo A string, <b>formatted as XML</b>, which contains additional error information.
+     * @param errorPath An instance identifier which contains error path
      */
     public RestconfError(final ErrorType errorType, final ErrorTag errorTag, final String errorMessage,
-                         final String errorAppTag, final String errorInfo, final YangInstanceIdentifier errorPath) {
+            final String errorAppTag, final String errorInfo, final YangInstanceIdentifier errorPath) {
         this.errorType = requireNonNull(errorType, "Error type is required for RestConfError");
         this.errorTag = requireNonNull(errorTag, "Error tag is required for RestConfError");
         this.errorMessage = errorMessage;
@@ -133,35 +113,38 @@ public class RestconfError implements Serializable {
      * Constructs a RestConfError object from an RpcError.
      */
     public RestconfError(final RpcError rpcError) {
-
         errorType = rpcError.getErrorType();
 
-        final ErrorTag tag = rpcError.getTag();
+        final var tag = rpcError.getTag();
         errorTag = tag != null ? tag : ErrorTag.OPERATION_FAILED;
 
         errorMessage = rpcError.getMessage();
         errorAppTag = rpcError.getApplicationTag();
+        errorInfo = rpcErrorInfo(rpcError);
+        errorPath = null;
+    }
 
-        String localErrorInfo = null;
-        if (rpcError.getInfo() == null) {
-            if (rpcError.getCause() != null) {
-                localErrorInfo = rpcError.getCause().getMessage();
-            } else if (rpcError.getSeverity() != null) {
-                localErrorInfo = "<severity>" + rpcError.getSeverity().elementBody() + "</severity>";
-            }
-        } else {
-            localErrorInfo = rpcError.getInfo();
+    private static String rpcErrorInfo(final RpcError rpcError) {
+        final var info = rpcError.getInfo();
+        if (info != null) {
+            return info;
         }
-
-        errorInfo = localErrorInfo;
-        errorPath = null;
+        final var cause = rpcError.getCause();
+        if (cause != null) {
+            return cause.getMessage();
+        }
+        final var severity = rpcError.getSeverity();
+        if (severity != null) {
+            return "<severity>" + severity.elementBody() + "</severity>";
+        }
+        return null;
     }
 
-    public ErrorType getErrorType() {
+    public @NonNull ErrorType getErrorType() {
         return errorType;
     }
 
-    public ErrorTag getErrorTag() {
+    public @NonNull ErrorTag getErrorTag() {
         return errorTag;
     }