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

import com.atlassian.bitbucket.mesh.rpc.v1.RpcDeleteRefFailed;
import com.atlassian.bitbucket.mesh.rpc.v1.RpcGitError;
import com.google.protobuf.ByteString;

import javax.annotation.Nonnull;

import static io.grpc.Status.INTERNAL;
import static java.util.Objects.requireNonNull;

public class DeleteRefFailedException extends AbstractGitException {

    private final String refName;

    public DeleteRefFailedException(@Nonnull String refName) {
        super("Failed to delete ref: " + refName, null, INTERNAL);

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

    public DeleteRefFailedException(@Nonnull String refName, @Nonnull Throwable cause) {
        super("Failed to delete ref: " + refName, cause, INTERNAL);

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

    @Nonnull
    public String getRefName() {
        return refName;
    }

    @Nonnull
    @Override
    public RpcGitError toError() {
        return RpcGitError.newBuilder()
                .setDeleteRefFailed(RpcDeleteRefFailed.newBuilder()
                        .setRefName(ByteString.copyFromUtf8(refName)))
                .build();
    }
}
