package com.atlassian.bitbucket.mesh.network;

import com.atlassian.bitbucket.mesh.AbstractStatusException;
import com.atlassian.bitbucket.mesh.rpc.v1.RpcError;
import com.atlassian.bitbucket.mesh.rpc.v1.RpcErrorUtils;
import com.atlassian.bitbucket.mesh.rpc.v1.RpcNoSuchRepository;
import io.grpc.Metadata;
import io.grpc.Status;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

import static java.util.Objects.requireNonNull;

public class NodeNotFoundException extends AbstractStatusException {

    private final String nodeId;

    public NodeNotFoundException(@Nonnull String nodeId, @Nullable String message) {
        super(message, Status.NOT_FOUND);

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

    @Nonnull
    public String getNodeId() {
        return nodeId;
    }

    @Override
    protected Metadata getTrailers() {
        return RpcErrorUtils.toMetadata(RpcError.newBuilder()
                .setNoSuchRepository(RpcNoSuchRepository.newBuilder()
                        .setId(nodeId))
                .build());
    }
}
