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

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

import javax.annotation.Nonnull;

import static java.util.Objects.requireNonNull;

public class InvalidTreeishException extends AbstractValidationException {

    private final String treeish;

    public InvalidTreeishException(@Nonnull String treeish) {
        super("[" + treeish + "] is not a valid tree identifier");

        this.treeish = requireNonNull(treeish, "id");
    }

    @Nonnull
    public String getTreeish() {
        return treeish;
    }

    @Nonnull
    @Override
    public RpcGitError toError() {
        return RpcGitError.newBuilder()
                .setInvalidTreeish(RpcInvalidTreeish.newBuilder()
                        .setTreeish(ByteString.copyFromUtf8(treeish)))
                .build();
    }
}
