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

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

import javax.annotation.Nonnull;

import static java.util.Objects.requireNonNull;

public class InvalidCommitishException extends AbstractValidationException {

    private final String commitish;

    public InvalidCommitishException(@Nonnull String commitish) {
        super("[" + commitish + "] is not a valid commit identifier");

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

    @Nonnull
    public String getCommitish() {
        return commitish;
    }

    @Nonnull
    @Override
    public RpcGitError toError() {
        return RpcGitError.newBuilder()
                .setInvalidCommitish(RpcInvalidCommitish.newBuilder()
                        .setCommitish(ByteString.copyFromUtf8(commitish)))
                .build();
    }
}
