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

import com.atlassian.bitbucket.mesh.rpc.v1.RpcCreateRefFailed;
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 CreateRefFailedException extends AbstractGitException {

    private final String refName;

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

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

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

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

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

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