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

import com.atlassian.bitbucket.mesh.rpc.v1.RpcGitError;
import com.atlassian.bitbucket.mesh.rpc.v1.RpcSetDefaultBranchFailed;
import com.google.protobuf.ByteString;
import io.grpc.Status;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

import static java.util.Objects.requireNonNull;

public class SetDefaultBranchFailedException extends AbstractGitException {

    private final String branchId;

    public SetDefaultBranchFailedException(@Nonnull String branchId, @Nullable Throwable cause) {
        super("Failed to set HEAD", cause, Status.INTERNAL);

        this.branchId = requireNonNull(branchId, "branchId");
    }

    @Nonnull
    public String getBranchId() {
        return branchId;
    }

    @Nonnull
    @Override
    public RpcGitError toError() {
        return RpcGitError.newBuilder()
                .setSetDefaultBranchFailed(RpcSetDefaultBranchFailed.newBuilder()
                        .setBranchId(ByteString.copyFromUtf8(branchId)))
                .build();
    }
}
