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

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

import javax.annotation.Nonnull;

import static java.util.Objects.requireNonNull;

public class PathNotFoundException extends AbstractNotFoundException {

    private final String commitish;
    private final String path;

    public PathNotFoundException(@Nonnull String commitish, @Nonnull String path) {
        super("Commit " + commitish + " has no such path: " + path);

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

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

    @Nonnull
    public String getPath() {
        return path;
    }

    @Nonnull
    @Override
    public RpcGitError toError() {
        return RpcGitError.newBuilder()
                .setNoSuchPath(RpcNoSuchPath.newBuilder()
                        .setCommitish(ByteString.copyFromUtf8(commitish))
                        .setPath(ByteString.copyFromUtf8(path)))
                .build();
    }
}
