package com.atlassian.bitbucket.mesh.git.exception;

import com.atlassian.bitbucket.mesh.git.builder.GitProcessBuilder;
import com.atlassian.bitbucket.mesh.rpc.v1.RpcGitError;
import com.atlassian.bitbucket.mesh.rpc.v1.RpcRevertFailed;

import javax.annotation.Nonnull;

import static io.grpc.Status.FAILED_PRECONDITION;
import static java.util.Objects.requireNonNull;
import static org.apache.commons.lang3.StringUtils.defaultString;

/**
 * An exception that is raised when the {@link GitProcessBuilder#revert() revert} operation fails.
 *
 * @since 2.1
 */
public class RevertException extends AbstractGitException {

    protected final String commitId;

    public RevertException(@Nonnull String message, @Nonnull String commitId) {
        super(requireNonNull(message, "message"), FAILED_PRECONDITION);

        this.commitId = commitId;
    }

    @Nonnull
    @Override
    public RpcGitError toError() {
        return RpcGitError.newBuilder()
                .setRevertFailed(RpcRevertFailed.newBuilder()
                .setCommitId(defaultString(commitId)))
                .build();
    }
}