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

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

import javax.annotation.Nonnull;

import static java.util.Objects.requireNonNull;

public class RefNotFoundException extends AbstractNotFoundException {

    private final String refName;

    public RefNotFoundException(@Nonnull String refName) {
        super("No such ref: " + refName);

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

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

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