From 1961de039cc598e0634dfecc0788218396ba6efc Mon Sep 17 00:00:00 2001 From: Robert Varga Date: Fri, 20 Oct 2023 19:42:25 +0200 Subject: [PATCH] Clean up RestconfError Update documentation and clean up conversion bits from RpcError. JIRA: NETCONF-1188 Change-Id: I446dd8e4c14473241838c95b5220aa7ec2253efd Signed-off-by: Robert Varga --- .../restconf/common/errors/RestconfError.java | 135 ++++++++---------- 1 file changed, 59 insertions(+), 76 deletions(-) diff --git a/restconf/restconf-common/src/main/java/org/opendaylight/restconf/common/errors/RestconfError.java b/restconf/restconf-common/src/main/java/org/opendaylight/restconf/common/errors/RestconfError.java index fbf66610ef..36949fd5c9 100644 --- a/restconf/restconf-common/src/main/java/org/opendaylight/restconf/common/errors/RestconfError.java +++ b/restconf/restconf-common/src/main/java/org/opendaylight/restconf/common/errors/RestconfError.java @@ -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. - * - *
- *
- * Note: Enumerations defined within are provided by the ietf restconf draft. + * Encapsulates a single {@code error} within the + * "errors" YANG Data Template. * * @author Devin Avery - * See also RESTCONF. */ 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, formatted as XML, 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, formatted as XML, 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, formatted as XML, 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, formatted as XML, 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 = "" + rpcError.getSeverity().elementBody() + ""; - } - } 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.elementBody() + ""; + } + return null; } - public ErrorType getErrorType() { + public @NonNull ErrorType getErrorType() { return errorType; } - public ErrorTag getErrorTag() { + public @NonNull ErrorTag getErrorTag() { return errorTag; } -- 2.36.6