package com.atlassian.bitbucket.mesh.repository;

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 RepositoryNotFoundException extends AbstractStatusException {

    private final String id;

    public RepositoryNotFoundException(@Nonnull String id, @Nullable String message) {
        this(id, message, null);
    }

    public RepositoryNotFoundException(@Nonnull String id, @Nullable String message, @Nullable Throwable cause) {
        super(message, cause, Status.NOT_FOUND);

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

    @Nonnull
    public String getId() {
        return id;
    }

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