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

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

    private final String refId;

    public EnableRefLogFailedException(@Nonnull String refId, @Nullable Throwable cause) {
        super("Failed to enable reflog for " + refId, cause, Status.INTERNAL);

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

    @Nonnull
    public String getRefId() {
        return refId;
    }

    @Nonnull
    @Override
    public RpcGitError toError() {
        return RpcGitError.newBuilder()
                .setEnableRefLogFailed(RpcEnableRefLogFailed.newBuilder()
                        .setRefId(ByteString.copyFromUtf8(refId)))
                .build();
    }
}
