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

import com.atlassian.bitbucket.mesh.rpc.v1.RpcGitError;
import com.atlassian.bitbucket.mesh.rpc.v1.RpcUnsupportedOperation;
import io.grpc.Status;

import javax.annotation.Nonnull;

import static java.util.Objects.requireNonNull;

/**
 * Exception that is raised when an operation is not supported
 */
public class UnsupportedOperationException extends AbstractGitException {

    private final String operation;

    public UnsupportedOperationException(@Nonnull String operation) {
        super("Unsupported operation: " + operation, Status.FAILED_PRECONDITION);

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

    @Nonnull
    public String getOperation() {
        return operation;
    }

    @Nonnull
    @Override
    public RpcGitError toError() {
        return RpcGitError.newBuilder()
                .setUnsupportedOperation(RpcUnsupportedOperation.newBuilder()
                        .setOperation(operation))
                .build();
    }
}
