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

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

import javax.annotation.Nonnull;

import static java.util.Objects.requireNonNull;

public class CommitNotFoundException extends AbstractNotFoundException {

    private final String commitId;

    public CommitNotFoundException(@Nonnull String commitId) {
        super("No such commit: " + commitId);

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

    @Nonnull
    public String getCommitId() {
        return commitId;
    }

    @Nonnull
    @Override
    public RpcGitError toError() {
        return RpcGitError.newBuilder()
                .setNoSuchCommit(RpcNoSuchCommit.newBuilder()
                        .setCommitish(ByteString.copyFromUtf8(commitId)))
                .build();
    }
}
