package com.atlassian.bitbucket.mesh.repair;

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.RpcRepositoryRepairSlotsExhausted;
import io.grpc.Metadata;
import io.grpc.Status;

import javax.annotation.Nonnull;

import static java.util.Objects.requireNonNull;

public class RepositoryRepairSlotsExhaustedException extends AbstractStatusException {

    public RepositoryRepairSlotsExhaustedException(@Nonnull String repositoryId) {
        super("Cannot start repair of " + requireNonNull(repositoryId, "repositoryId") +
                ": too many concurrent repairs", Status.RESOURCE_EXHAUSTED);
    }

    @Override
    protected Metadata getTrailers() {
        return RpcErrorUtils.toMetadata(RpcError.newBuilder()
                .setRepairSlotsExhausted(RpcRepositoryRepairSlotsExhausted.getDefaultInstance())
                .build());
    }
}
