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

import com.atlassian.bitbucket.mesh.git.exception.AbstractGitException;
import com.atlassian.bitbucket.mesh.rpc.v1.RpcGitError;
import com.atlassian.bitbucket.mesh.rpc.v1.RpcInvalidWorkTree;
import io.grpc.Status;

import javax.annotation.Nonnull;

import static java.util.Objects.requireNonNull;

public class InvalidWorkTreeException extends AbstractGitException {

    private final RpcInvalidWorkTree.Reason reason;

    public InvalidWorkTreeException(@Nonnull RpcInvalidWorkTree.Reason reason) {
        super("Invalid work tree: " + reason, Status.FAILED_PRECONDITION);
        this.reason = requireNonNull(reason, "reason");
    }

    @Nonnull
    @Override
    public RpcGitError toError() {
        return RpcGitError.newBuilder()
                .setInvalidWorkTree(RpcInvalidWorkTree.newBuilder().setReason(reason))
                .build();
    }
}
