*/
package org.opendaylight.restconf.common.errors;
-import com.google.common.base.Preconditions;
+import static java.util.Objects.requireNonNull;
+
import com.google.common.collect.ImmutableList;
import java.util.ArrayList;
import java.util.Collection;
import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.Nullable;
import org.opendaylight.restconf.common.errors.RestconfError.ErrorTag;
-import org.opendaylight.restconf.common.errors.RestconfError.ErrorType;
+import org.opendaylight.yangtools.yang.common.ErrorType;
import org.opendaylight.yangtools.yang.common.OperationFailedException;
import org.opendaylight.yangtools.yang.common.RpcError;
+import org.opendaylight.yangtools.yang.common.YangError;
import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
+import org.opendaylight.yangtools.yang.data.api.codec.YangInvalidValueException;
/**
* Unchecked exception to communicate error information, as defined in the ietf restcong draft, to be sent to the
* A string which provides a plain text string describing the error.
*/
public RestconfDocumentedException(final String message) {
- this(message, RestconfError.ErrorType.APPLICATION, RestconfError.ErrorTag.OPERATION_FAILED);
+ this(message, ErrorType.APPLICATION, RestconfError.ErrorTag.OPERATION_FAILED);
}
/**
*/
public RestconfDocumentedException(final String message, final ErrorType errorType, final ErrorTag errorTag,
final Throwable cause) {
- this(cause, new RestconfError(errorType, errorTag, message, null,
- cause.getMessage(), null));
+ this(cause, new RestconfError(errorType, errorTag, message, null, cause.getMessage(), null));
}
/**
* The underlying exception cause.
*/
public RestconfDocumentedException(final String message, final Throwable cause) {
- this(cause, new RestconfError(RestconfError.ErrorType.APPLICATION, RestconfError.ErrorTag.OPERATION_FAILED,
+ this(cause, new RestconfError(ErrorType.APPLICATION, RestconfError.ErrorTag.OPERATION_FAILED,
message, null, cause.getMessage(), null));
}
if (!errors.isEmpty()) {
this.errors = ImmutableList.copyOf(errors);
} else {
- this.errors = ImmutableList.of(new RestconfError(RestconfError.ErrorType.APPLICATION,
+ this.errors = ImmutableList.of(new RestconfError(ErrorType.APPLICATION,
RestconfError.ErrorTag.OPERATION_FAILED, message));
}
* the HTTP status.
*/
public RestconfDocumentedException(final Status status) {
- Preconditions.checkNotNull(status, "Status can't be null");
errors = ImmutableList.of();
- this.status = status;
+ this.status = requireNonNull(status, "Status can't be null");
}
public RestconfDocumentedException(final Throwable cause, final RestconfError error) {
super(cause, error.getErrorTag().getStatusCode());
- Preconditions.checkNotNull(error, "RestconfError can't be null");
errors = ImmutableList.of(error);
status = null;
}
return obj;
}
+ /**
+ * Throw an instance of this exception if the specified exception has a {@link YangError} attachment.
+ *
+ * @param cause Proposed cause of a RestconfDocumented exception
+ */
+ public static void throwIfYangError(final Throwable cause) {
+ if (cause instanceof YangError) {
+ final YangError error = (YangError) cause;
+ throw new RestconfDocumentedException(cause, new RestconfError(error.getErrorType().toNetconf(),
+ // FIXME: this is a special-case until we have YangError.getTag()
+ cause instanceof YangInvalidValueException ? ErrorTag.INVALID_VALUE : ErrorTag.MALFORMED_MESSAGE,
+ error.getErrorMessage().orElse(null), error.getErrorAppTag().orElse(null)));
+ }
+ }
+
private static List<RestconfError> convertToRestconfErrors(final Collection<? extends RpcError> rpcErrors) {
final List<RestconfError> errorList = new ArrayList<>();
if (rpcErrors != null) {